VSCode中无法捕捉wpf程序运行异常
1、按如下操作,编译成功后,但是运行后,没有按预期弹出主窗体
PS D:\test\TestPull\helloworld\Firstwpf> dotnet build
还原完成(0.2)
FirstWpf net472 win-x86 已成功 (0.1 秒) → bin\Debug\net472\FirstWpf.exe
在 0.8 秒内生成 已成功
PS D:\test\TestPull\helloworld\Firstwpf> dotnet run
PS D:\test\TestPull\helloworld\Firstwpf>
2、在主程序中加入异常捕捉程序
using System; using System.Windows; namespace FirstWpf; /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { public App() { // 捕获 UI 线程的异常 this.DispatcherUnhandledException += (sender, e) => { MessageBox.Show($"UI线程异常: {e.Exception.Message}\n\n堆栈:\n{e.Exception.StackTrace}", "启动错误", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; }; // 捕获非 UI 线程的异常 AppDomain.CurrentDomain.UnhandledException += (sender, e) => { MessageBox.Show($"运行时异常: {e.ExceptionObject.ToString()}", "启动错误", MessageBoxButton.OK, MessageBoxImage.Error); }; } }
3、成功捕捉异常

找到xaml文件中94-96行,发现调用了未定义的Style,删除掉
4、重新运行

------------------------------------
承接
**视觉检测软件开发及调试
**工业软件开发
**上位机软件开发
wechat:luoran2024
qq:565934058
email:taoyuansu@qq.com
海量教育资源及影视资源下载
微信公众号:EFunVision
------------------------------------

浙公网安备 33010602011771号