c# 开发的wpf程序闪退,无法用try catch捕获异常

2 篇文章 0 订阅
1 篇文章 0 订阅

之前开发的一个程序是c#wpf开发,基于.net framework 4.6.1的,一切都是正常的,但是在我重新装了win11后在程序logo出现后直接闪退,报错

返回值为 -1073740791 (0xc0000409),而且定位到代码时发现是,

public FAMainWindow()
        {
            InitializeComponent();

}

调用InitializeComponent()直接闪退,增加了try catch也无法捕获异常。

同时在 System.AppDomain.CurrentDomain.UnhandledException 这个事件里面也不没异常,十分的怪异,在闪退后也不打印堆栈信息。仅输出

finished with exit code -1073740791 (0xC0000409)

而且相同的程序在其它设备上正常运行,甚至在虚拟机中安装了相同版本的win11也正常运行。

这时我在管理工具-》事件查看器-》winodws日志中查询到出错的信息

错误应用程序名称: 检测系统.exe,版本: 4.0.3.0,时间戳: 0x665d6d68
错误模块名称: ucrtbase.dll,版本: 10.0.22621.3593,时间戳: 0x10c46e71
异常代码: 0xc0000409
错误偏移量: 0x000000000007f6fe
错误进程 ID: 0x0x2C64
错误应用程序启动时间: 0x0x1DAB585BA1E3B0E
错误应用程序路径: D:\working\heF\bin\x64\Debug\检测系统.exe
错误模块路径: C:\WINDOWS\System32\ucrtbase.dll
报告 ID: 93715dca-f599-4a5d-9835-1ed14f0f4827
错误程序包全名: 
错误程序包相对应用程序 ID: 

解决方法,直接重安装了显卡驱动,以及下面新的库,程序就正常运行了,代码没有改进。

参考

WPF意外无法启动?try-catch也无法捕捉?0xc0000409?_txt wpf file try catch-CSDN博客

WPF软件因中文名无法启动,原因竟是英特尔驱动 – 晨旭的博客~ (chenxublog.com)

好的,以下是一个使用WPF框架设计的计算BMI值的程序: 首先,我们需要在XAML中设计用户界面。在这里,我们将使用两个文本框和一个按钮来允许用户输入体重和身高,然后计算BMI值并显示结果。 ```xaml <Window x:Class="BMICalculator.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="BMI Calculator" Height="250" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Grid.Row="0" Content="Weight (kg):"/> <TextBox Grid.Row="0" Name="weightTextBox" Margin="5,0,0,0" /> <Label Grid.Row="1" Content="Height (m):"/> <TextBox Grid.Row="1" Name="heightTextBox" Margin="5,0,0,0" /> <Button Grid.Row="2" Content="Calculate BMI" Name="calculateButton" Margin="5,10,5,0" Click="CalculateButton_Click"/> <Label Grid.Row="3" Name="bmiResultLabel" Margin="5,10,5,5"/> </Grid> </Window> ``` 然后,我们需要编写C#代码来计算BMI值并将其显示在标签中。在这个例子中,我们将在单击“计算BMI”按钮时执行计算,并将结果显示在一个标签中。 ```csharp using System; using System.Windows; namespace BMICalculator { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void CalculateButton_Click(object sender, RoutedEventArgs e) { try { double weight = double.Parse(weightTextBox.Text); double height = double.Parse(heightTextBox.Text); double bmi = weight / (height * height); bmiResultLabel.Content = "Your BMI is: " + bmi.ToString("F2"); } catch (FormatException) { bmiResultLabel.Content = "Please enter valid numbers for weight and height."; } } } } ``` 这个程序很简单:当用户单击“计算BMI”按钮时,它将读取文本框中的体重和身高值,并使用这些值计算BMI。然后,它将BMI值显示在一个标签中。如果用户输入的不是数字,它将显示一个错误提示。 希望这可以帮助您开始编写BMI计算器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值