Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling. #1607

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions WinUIGallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Text;
using Windows.System;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.Uwp.Notifications;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to use the WinAppSDK Notifications instead from the Community Toolkit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it.

using static WinUIGallery.Win32;

namespace WinUIGallery
Expand Down Expand Up @@ -84,6 +85,7 @@ public static Window StartupWindow
public App()
{
this.InitializeComponent();
this.UnhandledException += HandleExceptions;

#if WINUI_PRERELEASE
this.Suspending += OnSuspending;
Expand Down Expand Up @@ -270,6 +272,31 @@ void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}

/// <summary>
/// Prevents the app from crashing when a exception gets thrown and notifies the user.
/// </summary>
/// <param name="sender">The app as an object.</param>
/// <param name="e">Details about the exception.</param>
private void HandleExceptions(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
e.Handled = true; //Don't crash the app.

//Create the notification.
var notification = new ToastContentBuilder()
.AddArgument("action", "viewConversation")
.AddArgument("conversationId", 9813);

//Set notification content.
notification.AddText("An exception was thrown.");
notification.AddText($"Type: {e.Exception.GetType()}");
notification.AddText($"Message: {e.Message}\r\n" +
$"HResult: {e.Exception.HResult}");

//Show the notification.
notification.Show();
}


#if WINUI_PRERELEASE
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
Expand Down
25 changes: 25 additions & 0 deletions WinUIGallery/WinUIGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@
<AppxBundle>Never</AppxBundle>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-Unpackaged|x86'" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are changes here intentional? If so, what is it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are changes here intentional? If so, what is it for?

Fixed it.


<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-Unpackaged|x64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-Unpackaged|ARM64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-Unpackaged|x86'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-Unpackaged|x64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-Unpackaged|ARM64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" />

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" />

<Import Project="Common.props" />

<ItemGroup>
Expand All @@ -98,6 +122,7 @@
just add versionless PackageReferences here. They will be updated to their actual versions by either the Packages.props file
in the WinUI repo, or the next ItemGroup below when standalone. -->
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="Microsoft.WindowsAppSDK" />
<PackageReference Include="ColorCode.Core" />
<PackageReference Include="Microsoft.Graphics.Win2D" />
Expand Down