Wahlin on .NET
Make Silverlight 3 Application Code More Compatible with Blend
Use the DesignProperties class to prevent code errors in the Blend designer.
Expression Blend 3 is a great tool for creating Silverlight or WPF user interfaces using design-time tools and controls. If you haven't tried version 3 of Expression Blend you're really missing out, since it adds a ton of new time-saving features.
While I really enjoy working in Blend, one of the things I've struggled with in the past is making code work better in Blend. If you've ever had an error like the following you know what I mean:
[Click on image for larger view.] |
What's up with the error? In short, I'm declaratively assigning my ViewModel object in the View's resources area, as shown below. If you are new to Model-View-View Model (MVVM) terminology, the ViewModel object contains the data being bound to the Silverlight View:
‹navigation:Page.Resources›
‹viewModel:PayrollSummaryViewModel x:Key="ViewModel" /›
‹/navigation:Page.Resources›
There's nothing wrong with that except that when the ViewModel object's constructor is called, an error is occurring due to a null reference exception. Here's the code in the constructor, which is calling out to a WCF service to retrieve some data:
public PayrollSummaryViewModel(IServiceProxy proxy)
{
_Proxy = (proxy != null) ? proxy : new ServiceProxy();
GetPayrollSummary();
}
You obviously can't call out to a WCF service when you don't have access to an HTTP stack. Fortunately, fixing the problem and making the code more "Blendable" is easy. Silverlight has a class named DesignerProperties that can be used to check if the code is being run in a designer such as Blend, or if the code is being run live. Here's an example of using the DesignerProperties class and wrapping it in a property named IsDesignTime:
public bool IsDesignTime
{
get
{
return DesignerProperties.GetIsInDesignMode(
Application.Current.RootVisual);
}
}
When the code is running in Blend, I can avoid trying to call the WCF service in the ViewModel object's constructor, by wrapping the code with the call to IsDesignTim, as shown below. Now Blend is happy with everything.
public PayrollSummaryViewModel(IServiceProxy proxy)
{
if (!this.IsDesignTime)
{
_Proxy = (proxy != null) ? proxy : new ServiceProxy();
GetPayrollSummary();
}
}
You can see that creating more "Blendable" code is pretty easy once you know this simple trick. More info on the
DesignerProperties class can be found here if you're interested.
About the Author
Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.