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

查看:168
本文介绍了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的设计视图中,我偶尔会收到此(无用的)错误消息:

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

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

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

解决方案

如果这是一致发生或半一致发生,则可以将调试器附加到XAML设计器:

  1. 启动Visual Studio;打开项目并打开XAML文件,导致XAML设计器加载

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

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

  4. Debug 菜单中,选择 Exception ... ,然后选中 Common Language旁边的 Thrown 复选框.运行时异常.这将启用对所有CLR异常的第一次机会处理.如果您知道例外的特定类型,则可以为该类型启用首次机会处理.

  5. 调试菜单中,选择附加到处理.在附加到:字段中,单击选择... ,然后检查列表中的托管(v4.5,v4.0)条目并点击确定.

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

  6. 可用进程列表框中,找到与您的项目相对应的 xdesproc.exe ,然后单击附加. >

    如果存在多个进程(通常是因为您打开了多个项目,或者因为设计器正在重新加载或最近重新加载),则可能很难确定哪个设计器进程属于哪个Visual Studio实例.仅附加到所有这些对象通常是最容易的.像流程浏览器之类的工具可以帮助您弄清楚哪个设计器流程属于哪个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天全站免登陆