WPF中的Data Binding调试指南

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

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

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”,给你:一套给力的 超级搜索力视频 课程!

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

在看+分享,人间真情 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309141.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算法-连续项的最大和问题(最大子数组问题)三种不同解法

连续项的最大和问题 &#xff08;1&#xff09;暴力求解 时间复杂度 O&#xff08;n^2&#xff09; &#xff08;2&#xff09;分治求解 时间复杂度 O&#xff08;nlogn&#xff09; &#xff08;3&#xff09; 线性求解 时间复杂度 O&#xff08;n&#xff09; C#代码 usin…

.NET 5 尝鲜 - 开源项目TerminalMACS WPF管理端支持.NET 5

点击上方“Dotnet9”添加关注哦聊天界面设计TerminalMACS一个使用 Prism 作为模块化框架、基于多个开源控件库作为UI控件选择、集成开源 UI 界面设计的 .NET 5 WPF 客户端项目。项目名称&#xff1a;TerminalMACS WPF管理端项目开源地址&#xff1a;Github&#xff1a;https://…

算法-排序-插入排序

插入排序 特点&#xff1a;原址排序&#xff0c;比较排序&#xff0c;&#xff08;最坏的情况&#xff09;时间复杂度O&#xff08;n^2&#xff09; #include <iostream>void insertion_sort(int *arr,int start,int end) {for (int i start1; i < end1; i) {int k…

MongoDB via Dotnet Core数据映射详解

用好数据映射&#xff0c;MongoDB via Dotnet Core开发变会成一件超级快乐的事。一、前言MongoDB这几年已经成为NoSQL的头部数据库。由于MongoDB free schema的特性&#xff0c;使得它在互联网应用方面优于常规数据库&#xff0c;成为了相当一部分大厂的主数据选择&#xff1b;…

算法-排序-归并排序

归并排序 特点&#xff1a;非原址排序&#xff0c;比较排序&#xff0c;时间复杂度O&#xff08;nlgn&#xff09; 稳定 // // Created by 许加权 on 2021/6/18. //#include <iostream> void merge_sort(int *arr,int start,int end) {if(start < end){int middle …

FreeSql.Generator命令行代码生成器是如何实现的

目录FreeSql介绍FreeSql.GeneratorRazorEngine.NetCore源码解析FreeSql.ToolsFreeSqlFreeSql 是功能强大的对象关系映射技术(O/RM)&#xff0c;支持 .NETCore 2.1 或 .NETFramework 4.0 或 Xamarin。有一个强大的ORM&#xff0c;也方便我们开发一个代码生成器。一般情况下&…

算法-排序-选择排序

选择排序 特点&#xff1a;原址排序&#xff0c;比较排序&#xff0c;时间复杂度O&#xff08;n^2&#xff09; // // Created by 许加权 on 2021/6/19. //#include <iostream>void selection_sort(int *arr,int start,int end) {for (int i start 1; i < end1; i…

.Net Core Configuration Etcd数据源

前言.Net Core为我们提供了一套强大的Configuration配置系统&#xff0c;使用简单扩展性强。通过这套配置系统我们可以将Json、Xml、Ini等数据源加载到程序中,也可以自己扩展其他形式的存储源。今天我们要做的就是通过自定义的方式为其扩展Etcd数据源操作。何为Etdc在使用etcd之…

算法-二分查找

二分查找 特点&#xff1a; T(n) T(n/2 - 1) c 时间复杂度O&#xff08;lgn) 前提&#xff1a;需要已排序的集合 int binary_search(int *arr,int start,int end,int key) {if(start<end){int middle (startend)/2;if(arr[middle] key) return middle;if(key<arr[m…

Java IDEA断点调试

断点调试(debug) 断点调试应用案例 01&#xff1a; package Assign;public class Debug01 {public static void main(String[] args) {int sum 0;for (int i 0;i<5;i){sumi;System.out.println(i);System.out.println(sum);}System.out.println("continue");} …

.NET Core请求控制器Action方法正确匹配,但为何404?

【导读】提前预祝各位端午节快乐。有时候我们会发现方法名称都正确匹配&#xff0c;但就是找不到对应请求接口&#xff0c;所以本文我们来深入了解下何时会出现接口请求404的情况。匹配控制器Action方法&#xff08;404&#xff09;首先我们创建一个web api应用程序&#xff0c…

布斯乘法以及带符号数的运算

乘法理解 对于最熟悉的十进制乘法很最基本的运算原理就是一个乘数乘以另一个乘数的个位、十位、百位数字然后求和。比如 放到二进制来看其实它也是这样的&#xff0c;多位数的乘法就是一个乘数乘上另一个乘数的各位求和。那么&#xff1a; 布斯算法及原理 原理 已经知道两…

算法-排序-冒泡排序

冒泡排序 特点&#xff1a;原址排序&#xff0c;比较排序 时间复杂度O&#xff08;n^2) void bubble_sort(int *arr,int start,int end) {for (int i start; i < end; i) {for (int j end; j >i ; j--) {if(arr[j-1]>arr[j]){int temp arr[j];arr[j] arr[j-1];a…

C++重载输出操作符<<,为什么要返回引用

针对&#xff1a;ostream & operator <<(ostream & os, const ClassType &object) 说明几点&#xff1a; 1.第一个形参为对ostream对象的引用&#xff0c;在该对象上将产生输出&#xff0c;ostream为非const&#xff0c;因为写入到流会改变流的状态&#x…

angular 接入 IdentityServer4

angular 接入 IdentityServer4Intro最近把活动室预约的项目做了一个升级&#xff0c;预约活动室需要登录才能预约&#xff0c;并用 IdentityServer4 做了一个统一的登录注册中心&#xff0c;这样以后就可以把其他的需要用户操作的应用统一到 IdentityServer 这里&#xff0c;这…

算法-计算逆序对个数

求逆序对的个数 特点&#xff1a;利用归并排序中合并的步骤&#xff0c;计算逆序对 时间复杂度O&#xff08;nlgn&#xff09; int merge_inversion(int *arr,int start,int end,int middle); int count_inversion(int *arr,int start,int end) {if(start<end){int middle…

主机Redis服务迁移到现有Docker Overlay网络

“《麻雀虽小&#xff0c;五脏俱全》之主机现有Redis服务迁移到Docker Swarm Overlay网络&#xff0c;并搭建高可用容器集群。hello, 好久不见&#xff0c;之前文章记录了一个实战的2C分布式项目的改造过程&#xff0c;结果如下&#xff1a;其中Redis并未完成容器化改造&#x…

堆排序(包含最大堆和最小堆)

堆排序&#xff08;包含最大堆和最小堆&#xff09; 堆排序 时间复杂度nlgn&#xff0c;原址排序。 通用函数 int parent(int i) {return (i - 1) >> 1; } int left(int i) {return (i << 1) 1; } int right(int i) {return (i << 1) 2; }最大堆排序 v…

Java控制结构

控制结构 程序流程控制介绍 顺序控制 分支控制if-else 单分支 案例演示 01: import java.util.Scanner; public class IfWorkDemo {public static void main(String[] args){Scanner myScanner new Scanner(System.in);System.out.println("input your age");int…

.Net Core Configuration源码探究

前言上篇文章我们演示了为Configuration添加Etcd数据源&#xff0c;并且了解到为Configuration扩展自定义数据源还是非常简单的&#xff0c;核心就是把数据源的数据按照一定的规则读取到指定的字典里&#xff0c;这些都得益于微软设计的合理性和便捷性。本篇文章我们将一起探究…