0

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.

5
  • stackoverflow.com/questions/78691633/… Commented Jul 23, 2024 at 6:09
  • Thank you for your response. The issue I'm facing is specifically about how to get into the UI thread and then show the dialog. I'm not able to get the current UI thread to update with the error message in my WinUI 3 App.xaml.cs file. Commented Jul 23, 2024 at 6:29
  • You don't need a Dispatcher to perform any of the functions in the links. If you're "showing async" it's not a "dialog"; it's a notification. An "alert". (See the link) Commented Jul 23, 2024 at 22:05
  • In WinUI 3's App.xaml.cs file, I encountered an error stating that 'DisplayAlert' does not exist in the current context. so I need to handle it in the similar way as done in above code(xamarin +uwp) Commented Jul 24, 2024 at 4:30
  • You may use AppDomain.CurrentDomain.UnhandledException to handle the exception or error on Windows platform, see stackoverflow.com/a/75453158/9644964 Commented Jul 29, 2024 at 2:50

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.