网站前端建设需要学会什么意思,二手车网站开发PPT,全屋定制十大名牌口碑,珠海网站制作报价点击蓝字“大白技术控”关注我哟加个“星标★”#xff0c;每日良时#xff0c;好文必达#xff01;WPF中的Data Binding如何Debug?大家平时做WPF开发#xff0c;相信用Visual studio的小伙伴比较多。XAML代码曾经在某些特殊版本的Visual Studio中是可以加断点进行调试的每日良时好文必达WPF中的Data Binding如何Debug?大家平时做WPF开发相信用Visual studio的小伙伴比较多。XAML代码曾经在某些特殊版本的Visual Studio中是可以加断点进行调试的不过目前多数版本都不支持在XAML加断点来调试。那如果自己需要绑定的 Property 没生效该怎么去检测或Debug排查问题呢下面大白给出几种自己用过的方法本人的开发环境是 Win10专业版x64 Visual Studio 2019专业版v16.2.2以下内容中给出了详细步骤的方法都亲测有效。方法1: 修改注册表 修改config文件在注册表中增加一个选项具体做法是在目录HKEY_CURRENT_USER\Software\Microsoft中创建文件夹Tracing, 然后在其里面创建子文件夹WPF然后新建一个DWORD(32位)值ManagedTracing将其值设置为1.注册表也可以将下面的文件另存为 trace.reg然后双击进行设置。Windows Registry Editor Version 5.00[HKEY_CURRENT_USER\Software\Microsoft\Tracing\WPF]
ManagedTracingdword:00000001
接下来需要在你的Project的能影响 .exe.config生成的那个 .config文件下加入折叠区域的内容:App.config由于我这边相关的config文件就是App.config所以只需在App.config中加入该内容。图中折叠的部分如下: system.diagnosticssourcessource nameSystem.Windows.Data switchNameBindingSwitch listenersadd nameBindingTextListener //listeners /source!--source nameSystem.Windows.Data switchNameBindingSwitch listenersadd nameBindingXmlListener //listeners /source--source nameSystem.Windows.DependencyProperty switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.Freezable switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.RoutedEvent switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.Media.Animation switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.NameScope switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.ResourceDictionary switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.Markup switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/sourcesource nameSystem.Windows.Documents switchNameBindingSwitch listenersadd nameBindingTextListener //listeners/source/sourcesswitchesadd nameBindingSwitch valueAll /!--add nameBindingSwitch valueOff --!--add nameBindingSwitch valueVerbose --!--add nameBindingSwitch valueWarning --!--add nameBindingSwitch valueActivity --/switchessharedListeners!-- This listener sends output to a file named BindingTrace.log (text) --add nameBindingTextListener typeSystem.Diagnostics.TextWriterTraceListener initializeDataBindingTrace.log /!-- This listener sends output to the console --add nameconsole typeSystem.Diagnostics.ConsoleTraceListener initializeDatafalse/!-- This listener sends output to an Xml file named BindingTrace.xml --add nameBindingXmlListener typeSystem.Diagnostics.XmlWriterTraceListener traceOutputOptionsNone initializeDataBindingTrace.xml //sharedListenerstrace autoflushtrue indentsize4/trace/system.diagnostics
设置好后你build这个wpf项目后当启动Debug时在其相应的debug目录下会多出一个 BindingTrace.log文件比如, 我这边的内容上这样的WPF binding - 日志文件我配置监听器(listener)时将debug的信息设置成了.log格式与.txt格式相比其优势是: 当用vs code打开时自带高亮看起来比较爽。 !-- This listener sends output to a file named BindingTrace.log (text) --add nameBindingTextListener typeSystem.Diagnostics.TextWriterTraceListener initializeDataBindingTrace.log /
当然也有小伙伴希望将Trace信息导出为xml也可以的只需将加入内容开头部分的: sourcelistenersadd nameBindingTextListener //listeners /source!--source nameSystem.Windows.Data switchNameBindingSwitch listenersadd nameBindingXmlListener //listeners/source --
改为: !-- sourcelistenersadd nameBindingTextListener //listeners /source --source nameSystem.Windows.Data switchNameBindingSwitch listenersadd nameBindingXmlListener //listeners/source
即可。那么此时在其相应的debug目录下会多出一个 BindingTrace.xml文件我这边的内容上这样的BindingTrace.xml参考:https://systemscenter.ru/scsm_authoringtool.en/html/b24efd85-0ced-48ea-8ecc-d816c789bae2.htmhttps://www.cnblogs.com/furenjun/archive/2011/08/01/2123983.htmlWPF Tutorial | Debug DataBinding Issueshttps://www.wpftutorial.net/DebugDataBinding.html45-DebuggingDataBinding · bstollnitz/old-wpf-bloghttps://github.com/bstollnitz/old-wpf-blog/tree/master/45-DebuggingDataBinding方法2: 在XAML中设置TraceLevel 在xaml中需要debug的View对应的 .xaml.cs文件中启用WPF Trace该方法适用于 .NET framework 3.5以后(包括 .NET core)的WPF project.首先需要给该View的xaml文件的某个节点加入PresentationTraceSources.TraceLevelHigh,UserControl x:ClassCaliburnMicro_Calculator.Views.CalculatorViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:localclr-namespace:CaliburnMicro_Calculator.Viewsxmlns:calhttp://www.caliburnproject.orgmc:IgnorabledWidth240
我这边直接在这个view的根节点UserControl中加入PresentationTraceSources.TraceLevelHigh结果如下:UserControl x:ClassCaliburnMicro_Calculator.Views.CalculatorViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:localclr-namespace:CaliburnMicro_Calculator.Viewsxmlns:calhttp://www.caliburnproject.orgmc:IgnorabledWidth240 PresentationTraceSources.TraceLevelHigh
此时我们还需要在目标View的对应的 .xaml.cs文件中启用WPF Trace. // Enable WPF tracingPresentationTraceSources.Refresh();PresentationTraceSources.DataBindingSource.Listeners.Add(new ConsoleTraceListener());PresentationTraceSources.DataBindingSource.Switch.Level SourceLevels.All;
此时在Output(输出窗口)就可以看到数据绑定的相关信息了。Output窗口可能有人会好奇output中的红色字体是怎么来的vs的output默认是黑色。其实安装一个vs插件VSColorOutput就好了传送门:https://marketplace.visualstudio.com/items?itemNameMikeWard-AnnArbor.VSColorOutput .当然你还可以在此时启用诊断工具位置是调试 - 窗口 - 显示诊断工具配合起来用起来更爽喔~VS中显示诊断工具方法3: Visual Studio 2019 (16.4之后的版本)安装 XAML binding extension这个VS插件由微软XAML团队推出看起来像是实现了方法1或方法2的自动化。XAML binding extension for Visual Studio 2019 下载地址:https://marketplace.visualstudio.com/items?itemNamePeterSpa.XamlBinding相关代码已开源:spadapet/xaml-binding-tool: XAML binding error window in a Visual Studio 2019 extensionhttps://github.com/spadapet/xaml-binding-tool当安装好这个插件时重启VS就可以用了debug时调试窗口中会多一个选项XAML binding failures (experimental)。点击该选项debug相关窗口中会显示Data binding的详细信息。XAML binding failures (experimental)此时WPF trace level附近的...还可以点击进行设置。XAML binding插件 设置方法4: 使用第三方debug工具首推Snoop这个工具大概2006年就出来了历史悠久最初由微软Blend团队的Pete Blois开发功能也异常强大而且目前也一直有Cory Plotts 等人负责维护和更新。Snoop主界面左上角支持filter属性或层级很多时可以快速定位目标节点。Snoop中的Tree, Properties, Data Context均支持filter(过滤搜索)而Properties和Data Context都可以打断点。当某个属性的值改变时整个属性的背景更改为黄色高亮一秒钟以吸引用户注意。Snoop允许你查看您在应用程序中指定的事件列表。当你单击元素时你可以看到哪些元素受到影响并查看哪个方法或任何人处理了该点击。Hanlded的事件以绿色显示。这是Snoop提供的查看隧道和事件冒泡传递之间的区别的强有力方法特别是当这些事件处理得太快或根本不处理它们如何影响您的可视化元素。当出现binding error时可以选择应用程序右侧的属性然后右键单击以深入了解绑定或绑定表达式以便给出更详细的错误说明。在Snoop的左上角有一个下拉框可以打开然后选择Show only Visuals with binding Errors以查看应用程序所具有的可视数据绑定错误列表。设置Show only Visuals with binding ErrorsSnoop 的一个众所周知的功能是能够识别数据绑定问题。当看到组件是否绑定正确时我通常只是尝试一下看看它是否有效。如果无效我转向 Visual Studio 调试模式下的output窗口。如果无法立即看到该值我会这样做将 Snoop 附加(Attach)到我的应用并从应用程序树视图上方的搜索/筛选器栏中选择Show only visuals with binding errors选项。Attach和Debug的步骤如下:以管理员权限启动snoop在代码里面的合适地方加上断点Ctrl F5 运行项目重现需要debug的界面调试 - Debug - 附加到进程(Attach)然后在snoop上依次点:Refresh按钮, Snoop按钮(望远镜)借助filter找需要inspect的目标元素接下来 debug就比较顺畅了。还可以使用它来显示任何具有绑定错误(Binding error)的控件就像word中的拼写检查一样Snoop 中的绑定错误会红色高亮显示也有小伙伴在用或WPF Inspector不过这个工具好久没更新了。WPF Inspector 这个项目之前是在CodePlex上的后来没人维护了目前有人手动fork到github上但没见任何更新。还有小伙伴用 Mole这个Visual Studio 插件对于这个插件Snoop的维护者Cory Plotts等人也有参与喔有兴趣的朋友可以去试试~Mole for Visual Studio插件下载:Mole for VS 2015 is installed from the Visual Studio Marketplacehttps://visualstudiogallery.msdn.microsoft.com/1d05cb44-8686-496b-9af3-4ed3deed3596.Mole for VS 2017 is installed from the Visual Studio Marketplacehttps://marketplace.visualstudio.com/items?itemNameKarlShifflettkdawg.MoleforVisualStudio2017.Mole for VS 2019 is installed from the Visual Studio Marketplacehttps://marketplace.visualstudio.com/items?itemNameKarlShifflettkdawg.MoleforVisualStudio2019.mole其他方法:将Binding改为x:Binding后进行调试增加一个 ValueConverter调用它进行调试这两种方法本人不太熟悉有兴趣的可以自己找相关资料去试试哈~ 有问题欢迎留言交流~End欢迎各位读者加入 .NET技术交流群在公众号后台回复“加群”或者“学习”即可。零基础玩视频号创作运营变现你要的干货都在这了C#刷遍Leetcode面试题系列连载1 - 入门与工具简介雷军1994年写的代码经典老古董~????这个春节的红包封面新姿势第二波文末彩蛋微信后台回复“asp”给你一份全网最强的ASP.NET学习路线图。回复“cs”给你一整套 C# 和 WPF 学习资源回复“core”给你2019年dotConf大会上发布的.NET core 3.0学习视频回复“so”给你一套给力的 超级搜索力视频 课程转发至朋友圈是对我最大的支持。在看分享人间真情