VS2012/Blend 5:调试设计视图中发生的异常(仅) [英] VS2012/ Blend 5: Debugging an Exception (only) occurring in design view

查看:30
本文介绍了VS2012/Blend 5:调试设计视图中发生的异常(仅)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 和 XAML 开发 Metro 风格的应用程序(适用于 Windows 8).我已将我的视图模型设置为用作设计时数据上下文,如下所示:

xmlns:vm="using:hub.ViewModels"d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=vm:ViewModels

我的应用在运行时似乎运行良好,但在 VS 2012 和 Blend 的设计视图中,我偶尔会收到以下(无用)错误消息:

抛出异常.TargetException:应用程序中的错误.堆栈跟踪在 System.ComponentModel.PropertyChangedEventHandler.Invoke(对象发送者,PropertyChangedEventArgs e)内部异常:无

发生在设计视图中 - 这意味着我无法围绕所有 INotifyPropertyChanged() 事件设置断点.

调试设计时错误的最佳方法是什么?

解决方案

如果这种情况持续或半持续发生,您可以将调试器附加到 XAML 设计器:

  1. 启动 Visual Studio;打开您的项目并打开一个 XAML 文件,从而加载 XAML 设计器

  2. 启动第二个 Visual Studio 实例.打开您的项目,但确保没有打开任何 XAML 文档.

  3. 确保禁用仅我的代码":从工具菜单中,选择选项.选择调试类别.在常规页面中,确保未选中Enable Just My Code旁边的复选框.

  4. Debug 菜单中,选择 Exceptions... 并选中 Common Language 旁边的 Thrown 复选框运行时异常.这将启用对所有 CLR 异常的第一次机会处理.如果您知道异常的具体类型,则可以仅针对该类型启用首次机会处理.

  5. 调试菜单中,选择附加到进程.在 Attach to: 字段中,单击 Select... 并检查列表中的 Managed (v4.5, v4.0) 条目并点击确定.

    这是必要的,因为如果在进程执行本机代码时附加,调试器可能会将进程误检测为本机进程.如果您的项目包含本机代码,您还需要选中列表中的 Native 复选框(您可以同时调试托管代码和本机代码).

  6. Available Processes列表框中,找到与您的项目对应的xdesproc.exe,然后点击Attach.p>

    如果有多个进程(通常是因为您打开了多个项目,或者因为设计器正在重新加载或最近重新加载),则可能很难确定哪个设计器进程属于哪个 Visual Studio 实例.连接到所有这些通常是最简单的.Process Explorer 之类的工具可以帮助您确定属于哪个设计器进程Visual Studio 的哪个实例.

    注意:不要将调试器附加到属于您用于调试的同一 Visual Studio 实例的设计器进程 (xdesproc):这样做可能会导致 Visual Studio 挂起.您必须始终使用两个不同的 Visual Studio 实例.

  7. 做任何你需要做的事情来重现这个错误.当它发生时,调试器将在抛出异常的地方中断.调试器应该为您的程序集加载符号.

I'm developing a Metro-style app (for Windows 8) using C# and XAML. I have set up my viewmodels to be used as design-time datacontexts, like so:

xmlns:vm="using:hub.ViewModels"
d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=vm:ViewModels

My app appears to work perfectly when run, but in the design views of both VS 2012 and Blend, I occasionally get this (unhelpful) error message:

An Exception was thrown. TargetException: Error in the application.
Stacktrace
at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
InnerException: None

This only happens in the design view - meaning I can't set breakpoints around all my INotifyPropertyChanged() events.

What is the best approach to debugging design-time errors?

解决方案

If this happens consistently or semi-consistently, you can attach the debugger to the XAML designer:

  1. Start Visual Studio; open your project and open a XAML file, causing the XAML designer to load

  2. Start a second instance of Visual Studio. Open your project but make sure no XAML documents are open.

  3. Ensure that Just My Code is disabled: From the Tools menu, select Options. Select the Debugging category. In the General page, ensure the check box next to Enable Just My Code is unchecked.

  4. From the Debug menu, select Exceptions... and check the Thrown check box next to Common Language Runtime Exceptions. This will enable first chance handling of all CLR exceptions. If you know the specific type of the exception, you can enable first chance handling for just that type.

  5. From the Debug menu, select Attach to Process. In the Attach to: field, click Select... and check the Managed (v4.5, v4.0) entry in the list and click OK.

    This is necessary because the debugger may misdetect the process as a native process if it attaches while the process is executing native code. If your project contains native code, you'll want to check the Native check box in the list as well (you can debug both managed and native code at the same time).

  6. In the Available Processes list box, find the xdesproc.exe that corresponds to your project and click Attach.

    If there are multiple processes (usually because you have multiple projects open or because the designer is reloading or has recently reloaded), it can be difficult to determine which designer process belongs to which Visual Studio instance. It's often easiest just to attach to all of them. A tool like Process Explorer can help you figure out which designer process belongs to which instance of Visual Studio.

    Note: Do not attach the debugger to a designer process (xdesproc) that belongs to the same instance of Visual Studio that you are using for debugging: doing so is likely to cause Visual Studio to hang. You must always use two different instances of Visual Studio.

  7. Do whatever you need to do to repro the bug. When it occurs, the debugger will break at the point where the exception is thrown. The debugger should load the symbols for your assemblies.

这篇关于VS2012/Blend 5:调试设计视图中发生的异常(仅)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆