调试 Visual Studio 设计器错误的好方法

发布于 2024-07-05 22:07:56 字数 180 浏览 7 评论 0原文

有没有好的方法可以调试 Visual Studio Designer 中的错误?

在我们的项目中,我们有大量的用户控件和许多复杂的表单。 对于复杂的情况,设计器经常抛出各种异常,这没有多大帮助,我想知道是否有一些好方法来找出出了问题。

语言是 C#,我们使用的是 Visual Studio 2005。

Is there a good way to debug errors in the Visual Studio Designer?

In our project we have tons of UserControls and many complex forms. For the complex ones, the Designer often throws various exceptions which doesn't help much, and I was wondering if there's some nice way to figure out what has gone wrong.

The language is C#, and we're using Visual Studio 2005.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

瑕疵 2024-07-12 22:07:56

我已经能够通过运行 VS 的第二个实例来调试一些控制设计器问题,然后从第一个 VS 实例执行“调试 -> 附加到进程”并选择“devenv”。

您将在第一个 VS 实例中设置断点。 使用第二个实例加载设计器以使“设计器”代码运行。

I've been able to debug some control designer issues by running a second instance of VS, then from your first VS instance do a "Debug -> Attach to Process" and pick "devenv".

The first VS instance is where you'll set your breakpoints. Use the second instance to load up the designer to cause the "designer" code to run.

如日中天 2024-07-12 22:07:56

我已经经历过很多次这种情况,这真的很痛苦。

首先,我建议尝试遵循设计者提供的堆栈跟踪,尽管我发现它通常只是列出一堆没有多大用处的内部内容。

如果这不起作用,请尝试编译并从那里确定异常。 你真的是盲目飞行,这就是问题所在。 然后,您可以尝试简单地运行代码并查看运行时引发的异常,这应该会为您提供更多信息。

最后的方法可能是从表单中删除所有非生成的代码,然后逐渐重新引入它以确定错误。

如果您使用自定义控件,如果之前的方法仍然导致错误,您也可以手动删除与自定义控件相关的生成代码。 然后,您可以以相同的方式重新逐步引入此步骤,以确定哪个自定义控件导致了问题,然后单独进行调试。

基本上据我所知,除了稍微解决一下问题之外,没有真正解决这个问题的方法!

I have had this happen many times and it is a real pain.

Firstly I'd suggest attempting to follow the stack trace provided by the designer, though I found that often simply lists a bunch of internals stuff that isn't much use.

If that doesn't work then try compiling and determining the exception from there. You really are flying blind which is the problem. You could then try simply running the code and seeing what exception is raised when you run it, that should give you some more information.

A last-gasp approach could be to remove all the non-generated code from the form and gradually re-introduce it to determine the error.

If you're using custom controls you could manually remove the generated code related to the custom controls as well if the previous method still results in an error. You could then re-introduce this step-by-step in the same way to determine which custom control is causing the problem, then go and debug that separately.

Basically as far as I can tell there's no real way around the problem other than to slog it out a bit!

笑饮青盏花 2024-07-12 22:07:56

这在 2005 年一直是一个痛苦的问题,在 2015 年仍然如此。断点通常不会命中,可能是因为程序集被影子复制或设计者的其他操作(?)。 您能做的最好的事情就是通过引入对 Debugger.Break() 的调用来手动中断。 您可以将其包装到编译器条件中,如下所示:

#if DEBUG
   System.Diagnostics.Debugger.Break(); 
#endif
int line_to = break; // <- if a simple breakpoint here does not suffice

It has been a pain in 2005 and still is in 2015. Breakpoints will often not hit, probably because of the assemblies being shadow copied or something by the designer(?). The best you can do is to break manually by introducing a call to Debugger.Break(). You may wrap it into a compiler conditional as so:

#if DEBUG
   System.Diagnostics.Debugger.Break(); 
#endif
int line_to = break; // <- if a simple breakpoint here does not suffice
一袭水袖舞倾城 2024-07-12 22:07:56

每一个都是不同的,有时可能会很晦涩难懂。 作为第一步,我将执行以下操作:

  • 使用源代码管理并经常保存。 当设计者发生错误时,获取最近发生的受影响控件的所有更改的列表,并测试每一项,直到找到罪魁祸首。
  • 请务必检查所涉及控件的初始化例程。 很多时候,这些错误的发生是由于通过控件的默认构造函数调用的某些错误或错误的依赖关系(这种错误可能只在 VS 中表现出来)

Each one is different and they can sometimes be obscure. As a first step, I would do the following:

  • Use source control and save often. When a designer error occurs, get a list of all changes to the affected controls that have occurred recently and test each one until you find the culprit
  • Be sure to check out the initialization routines of the controls involved. Very often these errors will occur because of some error or bad dependency that is called through the default constructor for a control (an error that may only manifest itself in VS)
谷夏 2024-07-12 22:07:56

我发现为什么有时断点没有被命中。 在“附加到进程”对话框中,“附加到:”类型必须为“选择...”。

当我更改为“托管 4.0、4.5”后,WinRT 应用程序的断点被命中。 来源:WinRT 中的设计器调试

I discovered why sometimes breakpoints are not hit. In the Attach to Process dialog, "Attach to:" type has to be "Select..."'d.

Once I changed to "Managed 4.0, 4.5", breakpoints for a WinRT application were hit. Source: Designer Debugging in WinRT.

幽蝶幻影 2024-07-12 22:07:56

您可以运行 VS 的第二个实例并将其附加到 VS 的第一个实例 (Ctrl+Alt+P)。 在第一个实例中设置断点,在第二个实例中运行设计器,断点将触发。 您可以单步执行代码,但“编辑并继续”将不起作用。

要使“编辑并继续”工作,请将控制库的调试选项设置为运行 VS,并将命令行参数作为解决方案文件名。 然后您只需设置断点并按 F5。 它将像用户代码一样进行调试! 附带说明一下,您也可以通过 VS 和 Office 加载项执行此操作。

You can run a second instance of VS and attach it to the first instance of VS (Ctrl+Alt+P). In the first instance set the breakpoints, in the second instance run the designer, and the breakpoint will fire. You can step through the code, but Edit-and-Continue will not work.

For Edit-and-Continue to work, set you control library's debug options to run a VS with the command line argument being the solution filename. Then you can simply set the breakpoints and hit F5. It will debug just like user code! As a side note, you can do this will VS and Office add-ins also.

眼眸里的快感 2024-07-12 22:07:56

这对我的 Visual Studio 2022 有用:

  • 我打开了第二个 Visual Studio 实例,
  • 在第二个实例中,我单击了“调试”->“调试”。 附加到进程...
  • 我从进程列表中选择了DesignToolsServer

更多详细信息:https://learn.microsoft.com/en-us/dotnet /desktop/winforms/controls/walkthrough-debugging-custom-windows-forms-controls-at-design-time?view=netframeworkdesktop-4.8

This worked for me for Visual Studio 2022:

  • I opened a second Visual Studio instance
  • In the second instance I clicked Debug -> Attach to Process...
  • I selected DesignToolsServer from the process list

More details: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/walkthrough-debugging-custom-windows-forms-controls-at-design-time?view=netframeworkdesktop-4.8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文