WPF教程之 异常处理

WPF应用程序:

在 WPF 中处理异常

如果你语言#可能其他任何你都可以配合 WPF 使用的 .NET,那么出现的情况对你来说可能不知道:当你有一段程序代码有机会抛出错误,通过「try-应该抓住」一部分包住它来了地方处理问题。举例说明,请思考下面的例子:

private void Button_Click(object sender, RoutedEventArgs e)
{
	string s = null;
	s.Trim();
}

继续,因为这个节目对一个现实是空的变数使用「Trim()」方法,会产生错误。你所见,这对人类相当不友善:

WPF教程之 异常处理

在这个约会对象中,用户会因为很容易陷入困境而不会发生的错误。所以如果你知道一些可能会出错的应用程序,你就应该使用“尝试捕捉”部分,例如:

private void Button_Click(object sender, RoutedEventArgs e)
{
	string s = null;
	try
	{
		s.Trim();
	}
	catch(Exception ex)
	{
		MessageBox.Show("A handled exception just occurred: " + ex.Message, "Exception Sample", MessageBoxButton.OK, MessageBoxImage.Warning);
	}
}

然而,意外是最容易的程序码也可能抛出异常,WPF让你全域处理所有处理的异常,而是以“试一试”的方式包覆整个行程码。 「应用」类别里的「DispatcherUnhandledException」事件来实现。如果订阅了该事件,用户有在程序码中未处理异常爆炸抛出时,WPF 将呼叫订阅的方法以下是一个基于我们的方法。讨论过东西的完整例子:

<Window x:Class="WpfTutorialSamples.WPF_Application.ExceptionHandlingSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ExceptionHandlingSample" Height="200" Width="200">
    <Grid>
        <Button HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click">
            Do something bad!
        </Button>
    </Grid>
</Window>
using System;
using System.Windows;

namespace WpfTutorialSamples.WPF_Application
{
	public partial class ExceptionHandlingSample : Window
	{
		public ExceptionHandlingSample()
		{
			InitializeComponent();
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			string s = null;
			try
			{
				s.Trim();
			}
			catch(Exception ex)
			{
				MessageBox.Show("A handled exception just occurred: " + ex.Message, "Exception Sample", MessageBoxButton.OK, MessageBoxImage.Warning);
			}
			s.Trim();
		}
	}
}

请注意这个我在「try-catch」一些外面额外呼叫了一次「修剪」方法,所以第一次呼叫有被处理,而第二次呼叫则没有。为了这个第二次呼叫,我们需要「App.xaml」的魔法。

<Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             DispatcherUnhandledException="Application_DispatcherUnhandledException"
             StartupUri="WPF Application/ExceptionHandlingSample.xaml">
    <Application.Resources>
    </Application.Resources>
</Application>
using System;
using System.Windows;

namespace WpfTutorialSamples
{
	public partial class App : Application
	{
		private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
		{
			MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "Exception Sample", MessageBoxButton.OK, MessageBoxImage.Warning);
			e.Handled = true;
		}
	}
}

WPF教程之 异常处理WPF教程之 异常处理

我们处理此区域区域性的处理方式与相当接近,仅在消息框中有非常多的文字和不同的图片。也请我将“e.处理”属性设置为“真实”,这将告知 WPF 我们已处理完成此异常且没有其他相关事项需要完成。

总结

处理是非常应用程序重要的一个部分,而任何地方 WPF 和 .NET 使所在区域或全域的异常处理都非常容易。你应该在合理的处理情况下采用区域性问题,并且只使用全域性问题处理作为最后的防线机制。

作者:冒牌SEO,如若转载,请注明出处:https://www.web176.com/wpf/16053.html

(0)
打赏 支付宝 支付宝 微信 微信
冒牌SEO冒牌SEO
上一篇 2023年4月19日
下一篇 2023年4月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注