如何传播错误&在WPF数据绑定期间发生的异常? [英] How to propagate errors & exceptions that occur during WPF data binding?

查看:71
本文介绍了如何传播错误&在WPF数据绑定期间发生的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现我的应用程序中意外破坏了数据绑定。通过重命名一个属性,而不是在XAML中重命名它,或者由于某种原因引起异常的属性。

Every so often I find that I have accidentally broken data binding in my application. Either by renaming a property and not renaming it in the XAML or by a property throwing an exception for some reason.

默认情况下,记录数据绑定错误以调试输出和异常被抛出的被捕获和抑制。

By default data binding errors are logged to debug output and exceptions that are thrown are caught and suppressed.

在调试输出被记录后,是否有一个简单的方法来抛出异常?

Is there an easy way to have an exception thrown after the debug output is logged?

我想尽快知道数据绑定是否被破坏(理想情况下在自动化测试中进行查找),并且不会冒险在人类测试之前不会被忽视的机会。

I want to know as soon as possible if data binding is broken (ideally picking it up in an automated test) and not risk the chance that it might go unnoticed until tested by a human.

推荐答案

经过一些拖延,我终于设定了解决方案的原始问题。

After some procrastination I finally set about coding a solution to my original issue.

我的解决方案使用自定义的 TraceListener (最初由John建议)记录到输出窗口。

My solution uses a custom TraceListener (originally suggested by John) that logs to an output window. The output window is automatically displayed and bought to the foreground when an error occurs.

这是我的 TraceListener

public class ErrorLogTraceListener : TraceListener
{
    public override void Write(string message)
    {
        ...
    }

    public override void WriteLine(string message)
    {
        ...
    }
}

TraceListener 在System.Diagnostics中定义。

TraceListener is defined in System.Diagnostics.

自定义 TraceListener 必须挂接到要使用的系统中。正确的方法是在注册表中设置一些内容,然后使用 App.config 文件配置 TraceListener

The custom TraceListener must be hooked into the system to be used. The official way to do this is to set something in the registry and then use the App.config file to configure the TraceListener.

然而,我发现有一个更简单的方法是以编程方式执行此操作:

However I found that there is a much easier way to do this programmatically:

ErrorLogTraceListener listener = new ErrorLogTraceListener();
PresentationTraceSources.Refresh();

PresentationTraceSources.DataBindingSource.Listeners.Add(listener);
PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error;

PresentationTraceSources 也在 System.Diagnostics

有关跟踪来源的更多信息,请参阅Mike Hillberg的博客

For more information on trace sources see Mike Hillberg's blog.

Bea Stollnitz有一些有用的有关她的博客的信息。

Bea Stollnitz has some useful info on her blog.

这篇关于如何传播错误&在WPF数据绑定期间发生的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆