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

  • Microsoft Revamps Fledgling AutoGen Framework for Agentic AI

    Only at v0.4, Microsoft's AutoGen framework for agentic AI -- the hottest new trend in AI development -- has already undergone a complete revamp, going to an asynchronous, event-driven architecture.

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

Subscribe on YouTube