c# - 如何在具有许多绑定(bind)的巨大 View 中找到错误的绑定(bind)?

标签 c# wpf debugging data-binding

我正在针对具有许多绑定(bind)的 View 运行 UI 测试。不幸的是,此测试失败并出现以下错误:

Test method MyExampleProject.UI.Tests.ExampleView.CancelCommand threw exception: 
System.Windows.Markup.XamlParseException: Two-way binding requires Path or XPath. ---> System.InvalidOperationException: Two-way binding requires Path or XPath.
    at System.Windows.Data.BindingExpression.CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent)
   at System.Windows.Data.BindingBase.ProvideValue(IServiceProvider serviceProvider)
   at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
   at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
   at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
   at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
   at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
   at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
   at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
   at System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)
   at System.Windows.FrameworkTemplate.<>c__DisplayClass45_0.<LoadOptimizedTemplateContent>b__2(Object sender, XamlObjectEventArgs args)
   at System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value)
   at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
   at System.Xaml.XamlObjectWriter.WriteEndObject()
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
 --- End of inner exception stack trace ---

Server stack trace: 
   at System.Threading.Thread.InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args)
   at System.Runtime.Remoting.Channels.CrossContextChannel.SyncProcessMessage(IMessage reqMsg)
Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at ..

据我理解错误,有双向绑定(bind),只读或者没有给定路径。这个 View 中有很多绑定(bind)。是否有指示器或其他东西可以帮助我找到错误的绑定(bind)?

最佳答案

我会推荐这个库:

NuGet:https://www.nuget.org/packages/WpfBindingErrors

GitHub:https://github.com/bblanchon/WpfBindingErrors

它有很大帮助,因为它可以将“绑定(bind)错误”转换为运行时异常。一旦代码中断,异常会告诉您到底是什么中断了。

只需通过 NuGet 安装它,并附加:

BindingExceptionThrower.Attach();

关于c# - 如何在具有许多绑定(bind)的巨大 View 中找到错误的绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40744722/

相关文章:

c# - .NET Core 使用哪个 C# 版本?

c# - DesignInstance 在 VS2013 中不起作用

wpf - 具有单行 WPF 的文本框

c# - AvalonDock 2.0 中的内容绑定(bind)

java - 使用嵌入式 Felix 进行调试

javascript - pdb.set_trace() 在网络浏览器 javascript 中是否等效?

c# - 如何在 C# 中替换字符串的第一个和最后一个字符?

c# - 从 txt 文件中读取每一行作为数组

c# - 如何使用 LINQ 按组选择具有重复列的记录?

c++ - 调试涉及使用 : vector, 字符串、STL 的 C++ 代码