加個(gè)“星標(biāo)★”,每日良時(shí),好文必達(dá)! WPF中的Data Binding如何Debug?大家平時(shí)做WPF開發(fā),相信用Visual studio的小伙伴比較多。XAML代碼曾經(jīng)在某些特殊版本的Visual Studio中是可以加斷點(diǎn)進(jìn)行調(diào)試的,不過目前多數(shù)版本都不支持在XAML加斷點(diǎn)來調(diào)試。 那如果自己需要綁定的 Property 沒生效,該怎么去檢測(cè)或Debug排查問題呢?下面大白給出幾種自己用過的方法,本人的開發(fā)環(huán)境是 Win10專業(yè)版x64 + Visual Studio 2019專業(yè)版v16.2.2,以下內(nèi)容中給出了詳細(xì)步驟的方法都親測(cè)有效。 方法1: 修改注冊(cè)表 + 修改config文件在注冊(cè)表中增加一個(gè)選項(xiàng), 具體做法是,在目錄 也可以將下面的文件另存為 trace.reg,然后雙擊進(jìn)行設(shè)置。 Windows Registry Editor Version 5.00
[ ] "ManagedTracing"=dword:00000001 接下來,需要在你的Project的能影響 由于我這邊相關(guān)的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> 設(shè)置好后,你build這個(gè)wpf項(xiàng)目后,當(dāng)啟動(dòng)Debug時(shí),在其相應(yīng)的debug目錄下會(huì)多出一個(gè) 我配置監(jiān)聽器(listener)時(shí),將debug的信息設(shè)置成了 <!-- This listener sends output to a file named BindingTrace.log (text) --> <add name="BindingTextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="BindingTrace.log" /> 當(dāng)然也有小伙伴希望將Trace信息導(dǎo)出為 <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> 即可。 那么,此時(shí)在其相應(yīng)的debug目錄下會(huì)多出一個(gè) 參考: 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中設(shè)置TraceLevel + 在xaml中需要debug的View對(duì)應(yīng)的 |
|