Well the full namespace for this method is System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me)
In the spirit of my last post discussing how Visual Studio 2010 (VS2010) lets the developers get access to a visual designer for Silverlight, lets look at why if you are doing Silverlight 3 with Visual Studio 2010 you need to get familiar with GetIsInDesignMode.
If you are like me, when my application opens I have it start loading data. I don’t typically open an empty application and then ask a user to take an action to load some data, I typically default to some level of data that the user will act on. Typically this is done by adding code to the Loaded event for your form or control.
This is still a valid implementation, however VS 2010 brings with it full designer support for Silverlight 3 (Silverlight 4 as well when it releases). As part of this when a Silverlight page/form is loaded into the designer, the designer fires the Loaded event to get any key data that is part of the running application. This is great because it is not uncommon to have certain key settings within this first event, key settings that are related to your design.
However, that also means code which by default loads data will also be called. That isn’t what most of us intend. In fact if your services are hosted within the same solution odds are good that your service isn’t running while you are in design mode. To avoid paying the price of having unnecessary code run when you put your XAML application (Silverlight 3 which you’ve probably built without a designer in particular) is to leverage GetIsInDesignMode().
Simply place your code within an if block like:
If Not System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me) Then
‘My custom Code
End If
Of course I’ve put all my code in Visual Basic syntax, so let me slow down a second and make sure I don’t leave C# developers out:
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
// My custom code;
}
Regardless of your implementation language, taking a moment to optimize your designer experience and keep from loading or trying to load default data while in the designer is a good idea.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.