Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've found something that works. The idea is use the <code>DesignerProperties.GetIsInDesignMode(...)</code> approach combined with <strong>checking for the process name that is running the code</strong>. </p> <p>For my VisualStudio 2010, I see that the process name is "devenv":</p> <p><img src="https://i.stack.imgur.com/DwjMC.png" alt="Visual Studio Process name"></p> <p>Then I found <a href="https://stackoverflow.com/a/2427420/718325">this post</a> which explains that the <code>System.Diagnostics.Process</code> is what we need to get at the process information. Knowing that, I created this helper method:</p> <pre><code>private bool IsVisualStudio2010DesignerRunning() { using (var process = System.Diagnostics.Process.GetCurrentProcess()) { const string visualStudio2010ProcessName = "devenv"; if (process.ProcessName.ToLowerInvariant().Contains(visualStudio2010ProcessName) &amp;&amp; DesignerProperties.GetIsInDesignMode(this)) { return true; } else return false; } } </code></pre> <h2>To illustrate that this is working, here is an example of its application</h2> <p>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 <code>OnApplyTemplate()</code> 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 <code>Storyboard</code> that is initiated by a call to <code>VisualStateManager.GoToState(...)</code>. </p> <p>To better illustrate that this is working, I'm setting the background property of the control to blue in the <code>OnApplyTemplate()</code> code that targets the VS 2010 designer (see screenshots).</p> <pre><code> /// 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); // ... } </code></pre> <p>Here's how Expression Blend 4's preview looks (notice the background of the SunkenBorder controls are not blue)...</p> <p><img src="https://i.stack.imgur.com/AbLYW.png" alt="Blend 4 designer preview"></p> <p>... and here's what Visual Studio's designer shows me. Now its designer is not crashed and the SunkenBorder controls' backgrounds are all blue...</p> <p><img src="https://i.stack.imgur.com/DU058.png" alt="Visual Studio 2010 designer preview"></p> <p>... and finally, here's the result at runtime (again the SunkenBorder controls' backgrounds are not blue):</p> <p><img src="https://i.stack.imgur.com/tpJAD.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload