Silverlight w/MVVM Pattern – Asynchronous Binding when loading a new view

I’ve been struggling on a part of the MVVM pattern implementation : basically ‘how do I update the view on the view’s load to bind to a control (like a list).  This is common senario where you are fetching data for the display.

Basically the binding seems to occur when the page is loaded and when the asynchronous call returns I’ve been unable to get the UI element to pick up the updated list (ie. observable list).  I don’t want to have any code in my view code behind, I’m 99% there…

After much trying and experimentation I found this article with sample code.

I’m going to take this sample, and build on it.. try it out on my sample test application…. keep your fingers crossed  :)  I’ll write back on the steps needed to do it… if it works!

UPDATE:  works perfectly.  This sample shows off a few other key items: 

  1. Dependency Injection + ServiceLocator 
  2. Commanding (SLExtensions) – basic commanding – I would like to use the Caliburn commanding, simply because I like that it handles just about any event… that will be my next stop.

UPDATE 2: the problem I described, I was able to figure it out when using Caliburn.

When the asynchronous call is completed it’s important to:

Previously I was doing this:

public void CompanyLoadingComplete(object sender, Model.EventArguments.CompanyLoadedEventArgs e)
        {
            CompanyList = e.Results;
        }

Then I tried this instead:

public void CompanyLoadingComplete(object sender, Model.EventArguments.CompanyLoadedEventArgs e)
        {
            CompanyList.Clear();
            foreach(var result in e.Results)
            {
                CompanyList.Add(result);
            }
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s