点击蓝字“大白技术控”关注我哟

加个“星标”,每日良时,好文必达!

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]
"ManagedTracing"=dword:00000001

接下来,需要在你的Project的能影响 .exe.config生成的那个 .config文件下加入折叠区域的内容:

App.config

由于我这边相关的config文件就是App.config,所以只需在App.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目录下会多出一个 BindingTrace.log文件,比如, 我这边的内容上这样的:

WPF binding - 日志文件

我配置监听器(listener)时,将debug的信息设置成了.log格式,与.txt格式相比其优势是: 当用vs code打开时,自带高亮,看起来比较爽。

      <!-- This listener sends output to a file named BindingTrace.log (text) --><add name="BindingTextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="BindingTrace.log" />

当然也有小伙伴希望将Trace信息导出为xml,也可以的,只需将加入内容开头部分的:

  <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目录下会多出一个 BindingTrace.xml文件,我这边的内容上这样的:

BindingTrace.xml

参考:

https://systemscenter.ru/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.wpftutorial.net/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对应的 .xaml.cs文件中启用WPF Trace

该方法适用于 .NET framework 3.5以后(包括 .NET core)的WPF project.

首先需要给该View的xaml文件的某个节点加入PresentationTraceSources.TraceLevel="High",

<UserControl x:Class="CaliburnMicro_Calculator.Views.CalculatorView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:CaliburnMicro_Calculator.Views"xmlns:cal="http://www.caliburnproject.org"mc:Ignorable="d"Width="240">

我这边直接在这个view的根节点<UserControl>中加入PresentationTraceSources.TraceLevel="High",结果如下:

<UserControl x:Class="CaliburnMicro_Calculator.Views.CalculatorView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:CaliburnMicro_Calculator.Views"xmlns:cal="http://www.caliburnproject.org"mc:Ignorable="d"Width="240" PresentationTraceSources.TraceLevel="High">

此时,我们还需要在目标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?itemName=MikeWard-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?itemName=PeterSpa.XamlBinding

相关代码已开源:

spadapet/xaml-binding-tool: XAML binding error window in a Visual Studio 2019 extension

https://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 Errors"

Snoop 的一个众所周知的功能是能够识别数据绑定问题。当看到组件是否绑定正确时,我通常只是尝试一下,看看它是否有效。如果无效,我转向 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 Marketplace

https://visualstudiogallery.msdn.microsoft.com/1d05cb44-8686-496b-9af3-4ed3deed3596.

Mole for VS 2017 is installed from the Visual Studio Marketplace

https://marketplace.visualstudio.com/items?itemName=KarlShifflettkdawg.MoleforVisualStudio2017.

Mole for VS 2019 is installed from the Visual Studio Marketplace

https://marketplace.visualstudio.com/items?itemName=KarlShifflettkdawg.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”,给你:一套给力的 超级搜索力视频 课程!

转发至朋友圈,是对我最大的支持。

在看+分享,人间真情 

WPF中的Data Binding调试指南相关推荐

  1. WPF中使用Data Annotations验证Model

    原文:WPF中使用Data Annotations验证Model .NET Framework中System.ComponentModel.DataAnnotations提供了很多属性来验证对象的属性 ...

  2. 解答WPF中ComboBox SelectedItem Binding不上的Bug

    原文:解答WPF中ComboBox SelectedItem Binding不上的Bug 正在做一个打印机列表,从中选择一个打印机(System.Printing) <ComboBox Widt ...

  3. Data Binding 用户指南(Android)

    1. 介绍 这篇文章介绍了如何使用Data Binding库来写声明的layouts文件,并且用最少的代码来绑定你的app逻辑和layouts文件. Data Binding库不仅灵活而且广泛兼容- ...

  4. Android开发教程 - 使用Data Binding(二)集成与配置

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  5. WPF中的数据绑定Data Binding使用小结

    完整的数据绑定的语法说明可以在这里查看: http://www.nbdtech.com/Free/WpfBinding.pdf MSDN资料: Data Binding: Part 1 http:// ...

  6. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

  7. Binding在WPF中的使用

    闲来无事,不想打DOTA,在这里小小研究下wpf中关于Binding的东西. 咯咯 在我们印象中,Binding的意思是"绑定",这个"绑"大概取自于Bind这 ...

  8. 转:WPF中图形表示语法详解(Path之Data属性语法)

    转自:http://blog.csdn.net/johnsuna/article/details/1885597 大可山 [MSN:a3news(AT)hotmail.com]  http://www ...

  9. WPF中ControlTemplate和DataTemplate的区别

    原文:WPF中ControlTemplate和DataTemplate的区别 下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window1 ...

最新文章

  1. android surface 平板,Surface平板能升级安卓4.0吗
  2. AAAI 2020 开源论文 | 可建模语义分层的知识图谱补全方法
  3. PHP的define与const
  4. JEPaas代码_((列表)输入字段值而计算
  5. java getscale_Java MajorType.getScale方法代碼示例
  6. 李天平×××作诞生记——《亮剑.NET:.NET深入体验与实战精要》
  7. 分裂的奶牛群(洛谷P2907题题解,Java语言描述)
  8. 2016年2月流量入口占比动态:搜索引擎大涨2.14%
  9. 计算机房安全等级标准,电子计算机房的分级标准
  10. java play database_Play Framework连接到数据库
  11. Php获取分类等级,PHP获取无限分类的完整等级列表
  12. 二叉树的前中后序遍历的三种实现(java)以及一些小习题
  13. 计算机主机房净高,机房建设标准
  14. 解决黑苹果核显HD4400开机卡在“io console user: gio screen lock state 3”问题/HD4400核显只有7M问题
  15. 顺德苹果售后维修体验
  16. emmm最近写了 那个图论入门 找最短路径 Prim 适合比较稠密的图 Kru 适合边比较少的图
  17. 2021年高处安装、维护、拆除考试及高处安装、维护、拆除考试技巧
  18. macbook买后配置
  19. 为什么把文字图片放大后有一圈彩色的像素方块,而不是纯黑色的
  20. Python爬取394452条《都挺好》弹幕数据,发现弹幕比剧还精彩?

热门文章

  1. Sharepoint Designer 2007 Workflow
  2. SQL server 2000安装时提示我”以前的某个程序安装已在安装计算机上创建挂起的文件操作....”...
  3. 为什么element的el-backtop会不管用,来看这里!
  4. python数据结构与算法第六讲_Python 学习 -- 数据结构与算法 (六)
  5. box-shadow阴影合集
  6. Oracle11g不能导出空表问题
  7. 清北·NOIP2017济南考前冲刺班 DAY1 morning
  8. ElasticSearch——学习笔记
  9. PHP提取字符串中的数字
  10. 分布式云+dubbo+zookeeper+Springmvc整合