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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube