VS2012/ Blend 5: Debugging an Exception (only) occurring in design view
Asked Answered
T

3

8

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?

Ten answered 31/7, 2012 at 10:50 Comment(0)
S
22

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.

Strontianite answered 3/8, 2012 at 23:18 Comment(4)
I agree with James, and great walkthrough. You can also attach Visual Studio to an open instance of Blend.Reifel
Thanks for the instructions - it revealed the exception to be a FileNotFoundException (Could not load file or assembly '_ExpressionDynamicAssembly_aeacad22-4ac3-4f3b-a146-564d51a81c8a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.). I'm going to assume it's a bug with design view - and see if it's fixed in the final version of VS!Ten
@rikkit: Great, I'm glad that helped. If you are still able to repro the bug in the Visual Studio 2012 RTM, please open a bug on Microsoft Connect.Strontianite
Update: I updated my tools to RTM. I still got the same bug, but fortunately the RTM design view now shows a full stacktrace - so it was my code after all. For anyone else with a similar problem, check that you're not trying to throw up login UI or message boxes in design view, because it just won't happen. :)Ten
C
5

I got here while looking for info on how to debug designer time instance problems, though I did not have the same problem as rikkit. But...I'm sharing the solution to a related issue just in case others having the same problem reach here as well:

Make sure you have the "Enable/Disable Project Code" toggle set to "enabled"...in VS/Blend 2015 it's a small icon below the XAML editor as shown here.

If it's disabled, this could be the reason your design time instance doesn't seem to be working.

Further, if it's disabled and you attempt to debug using a second VS instance, then when you attach to the XDesProc any breakpoints you set in the code-behind will report that they "will not currently be hit. [because] No symbols have been loaded for this document". You might think that you need to somehow load the symbols, but if you open up the Modules window attempting to do so, you won't even see your module in the listing.

I lost a couple hours on this issue because of this setting being disabled. Hope this helps others not do the same.

Cascarilla answered 22/6, 2016 at 20:18 Comment(0)
C
0

I'm not sure about it but I believe you can check for the IsInDesignModeProperty Field. I remeber having to do so on WinForms sometimes.

Also take a look at this link Troubleshooting WPF Designer Load Failures

Cozmo answered 3/8, 2012 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.