加个“星标★”,每日良时,好文必达! WPF中的Data Binding如何Debug?大家平时做WPF开发,相信用Visual studio的小伙伴比较多。XAML代码曾经在某些特殊版本的Visual Studio中是可以加断点进行调试的,不过目前多数版本都不支持在XAML加断点来调试。 那如果自己需要绑定的 Property 没生效,该怎么去检测或Debug排查问题呢?下面大白给出几种自己用过的方法,本人的开发环境是 Win10专业版x64 + Visual Studio 2019专业版v16.2.2,以下内容中给出了详细步骤的方法都亲测有效。 方法1: 修改注册表 + 修改config文件在注册表中增加一个选项, 具体做法是,在目录 也可以将下面的文件另存为 trace.reg,然后双击进行设置。 Windows Registry Editor Version 5.00
[ ] "ManagedTracing"=dword:00000001 接下来,需要在你的Project的能影响 由于我这边相关的config文件就是 图中折叠的部分如下: <system.diagnostics> <sources> <source name="System.Windows.Data" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source> <!--<source name="System.Windows.Data" switchName="BindingSwitch" > <listeners> <add name="BindingXmlListener" /> </listeners> </source>-->
<source name="System.Windows.DependencyProperty" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.Freezable" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.RoutedEvent" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.Media.Animation" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.NameScope" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.ResourceDictionary" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.Markup" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source>
<source name="System.Windows.Documents" switchName="BindingSwitch" > <listeners> <add name="BindingTextListener" /> </listeners> </source> </sources> <switches> <add name="BindingSwitch" value="All" /> <!--add name="BindingSwitch" value="Off" --> <!--add name="BindingSwitch" value="Verbose" --> <!--add name="BindingSwitch" value="Warning" --> <!--add name="BindingSwitch" value="Activity" --> </switches> <sharedListeners> <!-- This listener sends output to a file named BindingTrace.log (text) --> <add name="BindingTextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="BindingTrace.log" />
<!-- This listener sends output to the console --> <add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
<!-- This listener sends output to an Xml file named BindingTrace.xml --> <add name="BindingXmlListener" type="System.Diagnostics.XmlWriterTraceListener" traceOutputOptions="None" initializeData="BindingTrace.xml" /> </sharedListeners>
<trace autoflush="true" indentsize="4"></trace> </system.diagnostics> 设置好后,你build这个wpf项目后,当启动Debug时,在其相应的debug目录下会多出一个 我配置监听器(listener)时,将debug的信息设置成了 <!-- This listener sends output to a file named BindingTrace.log (text) --> <add name="BindingTextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="BindingTrace.log" /> 当然也有小伙伴希望将Trace信息导出为 <source> <listeners> <add name="BindingTextListener" /> </listeners> </source> <!--<source name="System.Windows.Data" switchName="BindingSwitch" > <listeners> <add name="BindingXmlListener" /> </listeners> </source> --> 改为: <!-- <source> <listeners> <add name="BindingTextListener" /> </listeners> </source> --> <source name="System.Windows.Data" switchName="BindingSwitch" > <listeners> <add name="BindingXmlListener" /> </listeners> </source> 即可。 那么,此时在其相应的debug目录下会多出一个 参考: https:///scsm_authoringtool.en/html/b24efd85-0ced-48ea-8ecc-d816c789bae2.htm https://www.cnblogs.com/furenjun/archive/2011/08/01/2123983.html WPF Tutorial | Debug DataBinding Issues https://www./DebugDataBinding.html 45-DebuggingDataBinding · bstollnitz/old-wpf-blog https://github.com/bstollnitz/old-wpf-blog/tree/master/45-DebuggingDataBinding 方法2: 在XAML中设置TraceLevel + 在xaml中需要debug的View对应的 |
|