【问题标题】:Exception on binding expression error绑定表达式错误的异常
【发布时间】:2011-06-02 19:04:57
【问题描述】:

如果在调试过程中发生绑定表达式错误,该错误将记录到 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')..

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

【问题讨论】:

    标签: wpf visual-studio-2010 debugging xaml binding


    【解决方案1】:

    您可以捕获跟踪错误。
    (监听器必须在外部 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>
    

    【讨论】:

    • 致 Avram:Silverlight 目前无法使用 (SL5)
    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 2021-10-09
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多