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、成功捕捉异常

image

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

4、重新运行

image

 

posted @ 2026-04-17 10:39  echo-efun  阅读(10)  评论(0)    收藏  举报