在 java 中,我们有 Thread.UncaughtExceptionHandler 用于处理未处理的异常。如何处理 C# UWP 中未处理的异常。
我无法使用 AppDomain,因为它不受支持。此外,还有一些关于程序集的参考。有没有我可以参考的例子或文件?
谢谢
在 java 中,我们有 Thread.UncaughtExceptionHandler 用于处理未处理的异常。如何处理 C# UWP 中未处理的异常。
我无法使用 AppDomain,因为它不受支持。此外,还有一些关于程序集的参考。有没有我可以参考的例子或文件?
谢谢
也许您正在寻找UnhandledException
您可以订阅OnUnhandledException
事件,并且如果UnhandledException
事件处理程序将Handled
事件参数的属性设置为true
,那么在大多数情况下,应用程序不会被终止。
由于我是 C# 新手,我发现使用该文档中的详细信息很难实现。我找到了一个链接如下,但这不适用于 UWP。msdn.microsoft.com/en-us/library/... 上面记录的示例的等效项对我很有帮助。
如果您创建一个新的空白 UWP 项目,您将找到该App.xaml.cs
文件。打开它并UnhandledException
为您的应用注册事件,如下所示:
sealed partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.UnhandledException += App_UnhandledException;
}
private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//TODO:
}
....
}
既然你说你是c#新手,那么你首先需要学习一些c#基础技术。例如,如何:订阅和取消订阅事件(C# 编程指南)