Reputation: 11
I'm migrating a Xamarin.Forms project with UWP to .NET MAUI with WinUI3 (multi project app).
I need to implement a similar global exception handling logic in my MAUI project with WinUI 3. Although I'm able to trigger the method when an unhandled exception occurs, showing a user dialog on top of the UI thread in the specific page where the issue occurred doesn't seem to work in WinUI 3.
In my Xamarin.Forms project, I handled global unhandled exceptions by logging them and displaying a dialog on the current page using the following code in App.xaml.cs:
Application.Current.UnhandledException += All_UnhandledException;
private async void All_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e) { try { var exception = e.Exception; var crashLog = "Unhandled Exception | Message: " + exception.Message + " | Target Method: " + exception.TargetSite.Name + " | Target Class: " + exception.TargetSite.DeclaringType.FullName;
Log.Fatal(crashLog, exception);
e.Handled = true;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
var showDialog = new MessageDialog("Caught the unhandled exception");
await showDialog.ShowAsync();
});
}
catch (Exception ex)
{
Log.Fatal($"Error while handling Unhandled exception: {ex.Message}");
}
}
How can I achieve this in a .NET MAUI project with WinUI 3? Any guidance or sample code would be greatly appreciated.
Upvotes: 0
Views: 168
Reputation: 1
Use ContentDialog control.
To know more, Refer this page on how to use ContentDialog
Also, you need to use Task.Async() and await for this.
Upvotes: 0