Silverlight 3 RIA controls and how they load


If you have started playing with the RIA controls for Silverlight, one thing that you will need to understand is that when you get a DomainContext and call one of the load commands, that it does this async.

So if you do something like:

var context = new TestDomainContext();
context.LoadProducts();
foreach (Product p in context.Products)
{
    myComboBox.Items.Add(p.ProductName);
}

You will get nothing put into your ComboBox.  You need to hook up to the Loaded event and when that fires, then you can load up your ComboBox:

var context = new TestDomainContext();
context.LoadProducts();
context.Loaded += new EventHandler
            <System.Windows.Ria.Data.LoadedDataEventArgs>
            (context_Loaded);

void context_Loaded(object sender,
            System.Windows.Ria.Data.LoadedDataEventArgs e)
{
    var context = sender as TestDomainContext;

    foreach (Product p in context.Products)
    {
        myComboBox.Items.Add(p.ProductName);
    }
}

Just wanted to get that out there in case anyone else runs into that issue.

Comments (4)

  1. Dennis Ward says:

    Tom – What if you are loading multiple tables asynchronously?  How can you tell what is loaded in the Loaded event?  Is there a 1 to 1 correspondence between calls to LoadXXX() and the Loaded event?  I’m trying to juggle multiple progress bars for different DataGrids, and am having trouble determining what object is being loaded…

    Any help is appreciated!

  2. Dennis,

    The event will be called each time one of the tables is loaded.  For me, I just made sure to check the to see if the context is still loading:

    if (!context.IsLoading)

    ..

    then I know everything is loaded.  You can also then use a counter to know how many times it has been called.

  3. Cliff says:

    Thanks Tom,

    I had been struggling with this and your succinct comments and code have resolved my confusion.

  4. Ian says:

    This is great, really appreciate information.

Skip to main content