如何屏蔽MAUI Android调试时的原生运行时异常弹窗
如何屏蔽MAUI Android调试时的无用弹窗(Debug.Assert首次调用触发)
调试MAUI Android应用时,频繁弹出一个完全无用的弹窗——既看不到堆栈跟踪,也得不到任何有效信息。我已经明确问题成因并能复现,现在只需要彻底屏蔽这个弹窗。
我试过以下方法,但都无效:
- 开启「Tools\Options\Debugging\General\Enable Just My Code」
- 在「Exception Settings」中关闭所有「Break When Thrown」选项
- 给AppDomain注册未处理异常处理程序,排除了异常未捕获的可能:
public partial class App : Application { public App() { AppDomain.CurrentDomain.UnhandledException += (sender, e) => { var exceptionType = e.ExceptionObject.GetType().Name; Debug.Fail(exceptionType); }; AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded; AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; InitializeComponent(); MainPage = new AppShell(); } }
复现示例
在Android 12物理测试设备上,应用启动后首次调用System.Diagnostics.Debug.Assert时必然弹出这个窗口,后续调用会正常在Debug.Assert代码行中断(这才是调试需要的),但首次调用只会弹窗口,不会定位到代码:
protected override void OnAppearing() { base.OnAppearing(); if (!_loaded) { _loaded = true; Debug.Assert(DeviceInfo.VersionString != "12", "Version 12 first time only bug."); } }
半解决状态
我添加了自定义System.Diagnostics.TraceListener到System.Diagnostics.Trace.Listeners,现在能通过这个监听器中断并获取调用堆栈:
class TraceListenerEx : TraceListener { public override void Write(string message) { } public override void WriteLine(string message) { if (message.Contains("Microsoft.Maui.FontManager", StringComparison.OrdinalIgnoreCase)) { // 忽略特定日志 } else { switch (message.Replace("Fail: ", string.Empty).Trim()) { case "Version 12 first time only bug.": // 在此处断点获取堆栈 break; default: break; } } } }
但问题是弹窗总是先于断点出现,严重拖慢调试节奏。
求问有没有彻底屏蔽这个无用弹窗的方法?
内容的提问来源于stack exchange,提问作者IV.
相关产品推荐
相关产品推荐

