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.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube