对于未处理的异常,至少,我希望能够捕获详细信息并将其写入文件,以便进行后续的"调试取证".Windows商店应用程序中没有"OnTerminating"事件; 有没有合适的地方/方式来完成这样的?
请参阅下面的评论.这是一个不适合下面的补充:
即使在删除xaml片段时,我仍然得到错误的消息,甚至在清理和重建之后... ??? 2点击错误信息只需将我带到App.xaml的顶部,现在全部是:
<Application
x:Class="SpaceOverlays.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SpaceOverlays">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
关闭App.xaml并重建后,一切都很好...... ??? 哦,好吧 - 我想,一切都很好.
有趣的是,Windows Phone应用程序App.xaml.cs默认具有此处理程序:
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break(); …Run Code Online (Sandbox Code Playgroud) c# debugging exception-handling windows-8 windows-store-apps
有没有办法为 MAUI 应用程序设置全局错误处理程序?当我从 Visual Studio 启动我的应用程序时,它当前运行良好。但是,在所有模拟器上,如果我尝试从 shell 的应用程序图标启动应用程序,它会启动然后退出(在 iOS 上,我会收到故障转储)。我不知道这发生在哪里,所以想尝试捕获全局处理程序。
我查看了 builder.ConfigureMauiHandlers,但它似乎只适用于控件。您不能使用 UnhandledExceptionEventHandler 类型,这是我认为合适的类型。
我还尝试了 AppDomain 处理程序,但它似乎也不起作用:
AppDomain ad = AppDomain.CurrentDomain;
ad.UnhandledException += Ad_UnhandledException;
Run Code Online (Sandbox Code Playgroud)
这可能吗?我只找到了 WCF 的示例,但它们不起作用。
有没有办法在 Xamarin.Forms 应用程序中处理全局级别的异常?
目前我的应用程序有一个登录页面,其中有一个按钮“抛出异常”按钮绑定到“Exception_Clicked”方法。
private void Exception_Clicked(object sender, EventArgs e)
{
throw new Exception("ERROR MESSAGE");
}
Run Code Online (Sandbox Code Playgroud)
我想要完成的是让应用程序管理该方法抛出的异常,而无需在每个方法中显式放置 try-catch。
目前我知道全局异常处理可以在常规的 Web 和桌面应用程序中使用如下代码处理
public static class ExceptionHandlerHelper
{
public static void Register()
{
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
{
Console.WriteLine(eventArgs.Exception.ToString());
Logging.LogException(eventArgs.Exception, string.Empty);
};
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在 xamarin.forms 中做到这一点,它是如何工作的?
编辑 1 - 虽然Xamarin Cross 平台的全局异常处理中给出的答案非常接近,但不幸的是它并没有阻止应用程序关闭,并且只显示异常发生位置的日志。我试图实现的异常处理必须在异常发生时捕获异常并允许应用程序正常继续