wpf - 绑定(bind)表达式错误的异常

标签 wpf visual-studio-2010 debugging xaml binding

如果在调试过程中发生绑定(bind)表达式错误,则会将错误记录到 Visual Studio 的“输出”窗口中。它看起来像这样:

System.Windows.Data Error: BindingExpression path error: 'User' property not found
on 'MainPageVM' 'MainPageVM' (HashCode=38694667). BindingExpression: Path='User.FullName'
DataItem='MainPageVM' (HashCode=38694667); target element is 'System.Windows.Controls.TextBlock'
Name=''); target property is 'Text' (type 'System.String')..

有没有办法将此错误视为未处理的异常?如果发生绑定(bind)错误,我不希望我的 Silverlight 应用程序继续运行。

最佳答案

您可以捕获跟踪错误。
(监听器必须在外部 dll 中。)

namespace CustomTracer
{
    public class CustomTraceListener : TraceListener
    {
        public CustomTraceListener()
        {
        }

        public override void Write(string message)
        {
        }

        public override void WriteLine(string message)
        {
            if(Debugger.IsAttached)
                Debugger.Break();
        }
    }
}

将此添加到 app.config
<system.diagnostics>
    <sources>
      <source name="System.Windows.Data" switchName="OnlyErrors" >
        <listeners>
          <add name="textListener" type="CustomTracer.CustomTraceListener,CustomTracer"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name ="OnlyErrors" value ="Error"/>
    </switches>
  </system.diagnostics>

关于wpf - 绑定(bind)表达式错误的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4583743/

相关文章:

c# - 跟踪点有什么用途?

c# - 如何检测用于粘贴的 control + v

wpf - 在 WPF 按钮中显示点对点进度

c++ - 将表单数据直接插入到 PugiXML 文档中

node.js - 调试永远无法解决 promise /异步等待

debugging - Visual Studio 2015 .NET Core 1.0 自动构建

wpf - 如何在 MVVM 中实现 INotifyCollectionChanged 以重置、添加、删除、移动、替换 NotifyCollectionChangedAction

wpf - 如何在 WPF 中将内部控件的属性公开给其父 UserControl

c++ - VS2010 : afxcw. lib(appcore.obj):错误 LNK2001:无法解析的外部符号 ___wargv

.net - 无法将 Rhino Mocks 3.5 添加到 Visual Studio 2010 中的 .NET 2.0 项目