2

I've found that in many cases the designer in Visual Studio 2010 or in Expression Blend 4 will crash if, for example, a WPF UserControl contains a control does something in code based on the control's loaded event (like transition to a different Visual State via a call like VisualStateManager.GoToState(this, "AfterLoaded", true);).

My typical approach to solving these designer crashes is use the DesignerProperties.GetIsInDesignMode(this) approach in the control's contructor:

public MyControl()
{
    // prevent designer crashes
    if (DesignerProperties.GetIsInDesignMode(this))
        return;

    Loaded += MyControlLoaded;
    Unloaded += MyControlUnloaded;
    IsVisibleChanged += MyControlIsVisibleChanged;    
}

This approach targets both Visual Studio 2010 and Expression Blend 4 and enables me to have my design surface visible again. However, it also removes any design-time preview that a designer might provide for me (like with the above-mentioned VSM state change on the loaded event). Blend, in particular, is able to provide that preview for me in its designer (if I switch to a different Blend tab and then switch back to the original tab I see the loaded animation run). Furthermore, with some controls that I have not yet applied the above approach to, Visual Studio 2010's designer will crash, while Blend 4's designer will not. Thus what I would like to do is check for only Visual Studio 2010's designer so that I can let Blend's designer pass through and provide its previewing abilities.

The benefit of this ability would be that I save time by not needing to build and run the application as often (to see things like the loaded animation) since Blend's designer could give me its preview.

Community
  • 1
  • 1
Jason Frank
  • 3,842
  • 31
  • 35

3 Answers3

1

I've found something that works. The idea is use the DesignerProperties.GetIsInDesignMode(...) approach combined with checking for the process name that is running the code.

For my VisualStudio 2010, I see that the process name is "devenv":

Visual Studio Process name

Then I found this post which explains that the System.Diagnostics.Process is what we need to get at the process information. Knowing that, I created this helper method:

private bool IsVisualStudio2010DesignerRunning()
{
    using (var process = System.Diagnostics.Process.GetCurrentProcess())
    {
        const string visualStudio2010ProcessName = "devenv";

        if (process.ProcessName.ToLowerInvariant().Contains(visualStudio2010ProcessName)
            && DesignerProperties.GetIsInDesignMode(this))
        {
            return true;
        }
        else
            return false;
    }
}

To illustrate that this is working, here is an example of its application

Its in a custom control that I wrote called SunkenBorder. This control has a behavior where it transitions to a certain VisualState at its first opportunity, so that this state is its initial state that the user sees. This code executes in the OnApplyTemplate() override. Expression Blend 4 is able to handle and display this at runtime. Visual Studio 2010's designer on the other hand, crashes entirely, as it is unable to execute the Storyboard that is initiated by a call to VisualStateManager.GoToState(...).

To better illustrate that this is working, I'm setting the background property of the control to blue in the OnApplyTemplate() code that targets the VS 2010 designer (see screenshots).

    /// Non-static constructor
    public SunkenBorder()
    {
        // Avoid Visual Studio 2010 designer errors
        if (IsVisualStudio2010DesignerRunning())
            return;

        // Expression Blend 4's designer displays previews of animations 
        //  that these event handlers initiate!
        Initialized += new EventHandler(SunkenBorder_Initialized);
        Loaded += new RoutedEventHandler(SunkenBorder_Loaded);
        Unloaded += new RoutedEventHandler(SunkenBorder_Unloaded);

        IsVisibleChanged += new DependencyPropertyChangedEventHandler(SunkenBorder_IsVisibleChanged);
    }

    // ...

    /// Used to set the initial VSM state (its the first opportunity).
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();


        if (IsVisualStudio2010DesignerRunning())
        {
            // set a property just to illustrate that this targets only Visual Studio 2010:
            this.Background = Brushes.Blue;
            // return before doing VisualState change so Visual Studio's designer won't crash
            return;
        }
        // Blend 4 executes this at design-time just fine
        VisualStateManager.GoToState(this, "InitialState", false);

        // ...
    }

Here's how Expression Blend 4's preview looks (notice the background of the SunkenBorder controls are not blue)...

Blend 4 designer preview

... and here's what Visual Studio's designer shows me. Now its designer is not crashed and the SunkenBorder controls' backgrounds are all blue...

Visual Studio 2010 designer preview

... and finally, here's the result at runtime (again the SunkenBorder controls' backgrounds are not blue):

enter image description here

Community
  • 1
  • 1
Jason Frank
  • 3,842
  • 31
  • 35
0

You'd probably be better off crafting a design time view model. That would eliminate having to check for designers of any type in your control's constructor.

KodeKreachor
  • 8,852
  • 10
  • 47
  • 64
  • 1
    Thanks for your reply. While that approach may be useful is some circumstances, most of the cases that I'm dealing with are with _custom_ controls and they have no ViewModel (they don't need binding to data), so that approach doesn't seem to work for these cases. Nonetheless, that's a good link that shows how to generate a design-time ViewModel in Blend. Thanks again. – Jason Frank Jan 30 '13 at 18:27
0

(NOTE: Not in a position to test/verify this right now, will do so later)

It looks like the Silverlight folks went off the farm when doing their "In Designer" check, so now there are two properties:

  • IsInDesignMode, which seems to work consistently for VS2010

  • IsInDesignTool, which seems to work semi-consistently for Blend

Also found this:

http://www.arrangeactassert.com/how-to-use-the-designerproperties-isindesigntool-property-to-populate-controls-with-design-time-data-in-silverlight-using-expression-blend/

JerKimball
  • 16,584
  • 3
  • 43
  • 55
  • It appears that `IsInDesignTool` is only available for Silverlight, not WPF. I tried to direct the question to WPF with my tags and question context, but I'll add an edit to make it clearer. Also, I found [this](http://stackoverflow.com/questions/8869721/whats-the-difference-between-designerproperties-isindesignmode-and-designerprop) regarding the differences between those two properties. It looks like the main difference may just be that `IsInDesignTool` is for Silverlight. Thanks. – Jason Frank Jan 30 '13 at 19:58
  • @Jason Aww, too bad...was a shot in the dark. – JerKimball Jan 30 '13 at 20:10