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

查看:113
本文介绍了如何传播错误和放大器;在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 (最初由约翰建议),它记录到一个输出窗口。 。自动显示输​​出窗口,买了到发生错误时前台

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 也在<$ C定义。$ C> System.Diagnostics程序

有关跟踪源的详细信息请参见迈克Hillberg的的博客

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

比亚Stollnitz有一些有用的信息在她的博客

Bea Stollnitz has some useful info on her blog.

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

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