简体   繁体   English

如何在设计模式下调试 WPF 用户控件?

[英]How do you debug a WPF user control in design mode?

I have a wpf user control I created that contains a label.我创建了一个包含标签的 wpf 用户控件。 The label's foreground setting changes based on some code that checks a number of conditions.标签的前台设置会根据一些检查多个条件的代码而改变。 The label in my control is displaying the wrong color at design time but I cannot figure out how to debug my user control at design time so that I can have it catch breakpoints (and thus figure out where my logic is flawed).我的控件中的标签在设计时显示错误的颜色,但我无法弄清楚如何在设计时调试我的用户控件,以便我可以让它捕获断点(从而找出我的逻辑有缺陷的地方)。 The color on the control is correct at run-time, it is only at design time that it is displayed incorrectly.控件上的颜色在运行时是正确的,只是在设计时显示不正确。

Here are the steps to debug a control at design time:以下是在设计时调试控件的步骤:

  • Start a second instance of Visual Studio启动 Visual Studio 的第二个实例
  • Attach to the first instance from the Debug menu (or in VS2022, attach to the WPFSurface)从 Debug 菜单附加到第一个实例(或在 VS2022 中附加到 WPFSurface)
  • In the second instance, open the source code of your control在第二种情况下,打开您的控件的源代码
  • Set breakpoints at appropriate locations在适当的位置设置断点
  • In the first VS instance, reload the designer在第一个 VS 实例中,重新加载设计器

Note that this technique isn't limited to controls;请注意,此技术不仅限于控件; you can also use it to debug VS extensions, or anything that executes within Visual Studio.您还可以使用它来调试 VS 扩展,或在 Visual Studio 中执行的任何内容。

To debug Controls assembly, I use following solution.要调试控件程序集,我使用以下解决方案。

  1. open project's properties using Project menu使用项目菜单打开项目的属性

  2. display Debug (Déboguer in french)显示Debug (法语中的 Déboguer)

  3. Set Start extern program to devenv.exe (red rectangle)Start extern program设置为devenv.exe (红色矩形)

    On my PC it is在我的电脑上是

    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe

  4. Fill arguments of command's line so that is is equal to solution's file (blue rectangle)填充命令行的参数,使其等于解决方案的文件(蓝色矩形)

  5. Specify working directory.指定工作目录。 Since directory is not speficied in arguments, working directoring is equivalent to directory that contains .sln file.由于没有在参数中指定目录,因此工作目录等同于包含.sln文件的目录。 (green rectangle) (绿色矩形)

    On my PC it is在我的电脑上是

    D:\@Projets\VisualBasic.Net\Tramex.Controls.Net\Test

在此处输入图像描述

Now, the first time I start debugging, a new Visual Studio is started.现在,我第一次开始调试时,会启动一个新的 Visual Studio。 This phase can leave some minutes (on my PC 5 minutes).这个阶段可能需要几分钟(在我的电脑上需要 5 分钟)。 But this is only for the first time I start the debug process.但这只是我第一次开始调试过程。

I can then works with controls in Designer on second Visual Studio application and see what happens when a breakpoint define in first windows is raised.然后,我可以在第二个 Visual Studio 应用程序上使用 Designer 中的控件,并查看在第一个窗口中定义的断点引发时会发生什么。

This works perfectly well with WinForms controls.这与 WinForms 控件完美配合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在设计模式下显示/隐藏用户控件 - How do show/hide user control in design mode 您如何从基本WPF用户控件继承从属属性到继承的新用户控件? - How do you inherit Dependent Properties from a base WPF User Control to a new User Control that is inherited? 在WPF中,如何从嵌套用户控件访问Window.xaml中定义的对象? - In WPF how do you access an object defined in Window.xaml from a nested user control? WPF设计:如何使用圆角和阴影效果进行用户控制 - WPF Design: How to make User control with rounded corner and shadow effect 如何绑定到用户控件的属性? - How do you bind to the property of a User Control? 怎么用WPF中的Frame控件做过渡效果? - How do you do transition effects using the Frame control in WPF? 用户控件包含子控件,设计模式编辑子控件 - User control containing child control, design mode edit child control WPF用户控件设计时间访问内部用户控件 - WPF User Control design time accessing internal user control 如何覆盖不在(xaml / wpf)中的网格上的控件 - How do you overlay a control over a grid that it is not in (xaml/wpf) 您如何在WPF日历控件上检测到自动的Month转换? - How do you detect an automatic Month shift on the WPF Calendar control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM