visual-studio 调试 Visual Studio 设计器错误的好方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good Way to Debug Visual Studio Designer Errors
提问by Daisuke Shimamoto
Is there a good way to debug errors in the Visual Studio Designer?
是否有调试 Visual Studio 设计器中的错误的好方法?
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.
在我们的项目中,我们有大量的 UserControl 和许多复杂的表单。对于复杂的,设计器经常抛出各种没有多大帮助的异常,我想知道是否有一些很好的方法来找出出了什么问题。
The language is C#, and we're using Visual Studio 2005.
语言是 C#,我们使用的是 Visual Studio 2005。
采纳答案by Daisuke Shimamoto
See Debugging Design-Time Controls(MSDN).
请参阅调试设计时控件(MSDN)。
回答by Craig
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".
我已经能够通过运行 VS 的第二个实例来调试一些控件设计器问题,然后从您的第一个 VS 实例执行“调试 - > 附加到进程”并选择“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.
第一个 VS 实例是您设置断点的地方。使用第二个实例加载设计器以运行“设计器”代码。
回答by Haymo Kutschbach
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:
2005 年一直很痛苦,2015 年仍然如此。断点通常不会命中,可能是因为组件被影子复制或设计者(?)。您能做的最好的事情是通过引入对 的调用来手动中断Debugger.Break()。您可以将其包装到编译器条件中,如下所示:
#if DEBUG
System.Diagnostics.Debugger.Break();
#endif
int line_to = break; // <- if a simple breakpoint here does not suffice
回答by ljs
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!
基本上据我所知,除了稍微解决这个问题之外,没有其他真正的方法可以解决这个问题!
回答by ShawnFeatherly
I discovered why sometimes breakpoints are not hit. In the Attach to Processdialog, "Attach to:" type has to be "Select..."'d.
我发现为什么有时断点没有命中。在“附加到进程”对话框中,“附加到:”类型必须为“选择...”。
Once I changed to "Managed 4.0, 4.5", breakpoints for a WinRTapplication were hit. Source: Designer Debugging in WinRT.
一旦我更改为“Managed 4.0, 4.5”,就会命中WinRT应用程序的断点。来源:WinRT 中的设计器调试。
回答by Kevin Phelps
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.
您可以运行 VS 的第二个实例并将其附加到 VS 的第一个实例 (Ctrl+Alt+P)。在第一个实例中设置断点,在第二个实例中运行设计器,断点将被触发。您可以单步执行代码,但“编辑并继续”将不起作用。
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.
要使“编辑并继续”工作,请将控制库的调试选项设置为运行 VS,命令行参数为解决方案文件名。然后你可以简单地设置断点并按 F5。它会像用户代码一样调试!作为旁注,您也可以在 VS 和 Office 加载项中执行此操作。
回答by Yaakov Ellis
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)
- 使用源代码控制并经常保存。当发生设计器错误时,获取最近发生的受影响控件的所有更改的列表,并测试每个更改,直到找到罪魁祸首
- 请务必检查所涉及控件的初始化例程。很多时候,这些错误的发生是由于某些错误或通过控件的默认构造函数调用的错误依赖(这种错误可能只在 VS 中表现出来)

