Search code examples
c#wpfvisual-studio-2010visual-studioexpression-blend

How do you check for ONLY the Visual Studio 2010 designer (so that Blend's designer is NOT targeted) in WPF?


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.


Solution

  • 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