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

  • Mastering Blazor Authentication and Authorization

    At the Visual Studio Live! @ Microsoft HQ developer conference set for August, Rockford Lhotka will explain the ins and outs of authentication across Blazor Server, WebAssembly, and .NET MAUI Hybrid apps, and show how to use identity and claims to customize application behavior through fine-grained authorization.

  • Linear Support Vector Regression from Scratch Using C# with Evolutionary Training

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the linear support vector regression (linear SVR) technique, where the goal is to predict a single numeric value. A linear SVR model uses an unusual error/loss function and cannot be trained using standard simple techniques, and so evolutionary optimization training is used.

  • Low-Code Report Says AI Will Enhance, Not Replace DIY Dev Tools

    Along with replacing software developers and possibly killing humanity, advanced AI is seen by many as a death knell for the do-it-yourself, low-code/no-code tooling industry, but a new report belies that notion.

  • Vibe Coding with Latest Visual Studio Preview

    Microsoft's latest Visual Studio preview facilitates "vibe coding," where developers mainly use GitHub Copilot AI to do all the programming in accordance with spoken or typed instructions.

  • Steve Sanderson Previews AI App Dev: Small Models, Agents and a Blazor Voice Assistant

    Blazor creator Steve Sanderson presented a keynote at the recent NDC London 2025 conference where he previewed the future of .NET application development with smaller AI models and autonomous agents, along with showcasing a new Blazor voice assistant project demonstrating cutting-edge functionality.

Subscribe on YouTube