Making Silverlight 3 Application Code More Compatible with Blend

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 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:

image

What’s up with the error?  In short, I’m declaratively assigning my ViewModel object (my object that contains the data being bound to the Silverlight View if you’re new to the whole MVVM terminology) in the View’s resources area as shown next:

<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.  Note: Some people like the declarative way of defining ViewModels and some people don't.  A few months ago I was against that approach until I started working on my current project and realized that the pros outweighed the cons (at least for my scenario).  Plus, the declarative approach is used when working with test data in Blend 3.  Ultimately each application has different requirements so I'll leave it as an "exercise for the reader" to decide what works best for you.

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);
    }
}


To avoid trying to call the WCF service in the ViewModel object’s constructor when the code is run in Blend I can wrap the code with the call to IsDesignTime as shown next and 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.  There are apparently some issues using it with the Visual Studio designer used for Silverlight 2, but since Silverlight 3 doesn’t have a Visual Studio designer that’s kind of a moot point.

comments powered by Disqus

1 Comment

  • I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
    Susan

Comments have been disabled for this content.