如果在调试过程中发生绑定(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/