Skip Navigation LinksHome / Articles / View Article

Tip: Binding and getting data async from a Web Service

+ Add to SilverlightShow Favorites
1 comments   /   posted by Emil Stoychev on Aug 12, 2008
(4 votes)
Categories: Learn , Tips and Tricks

Introduction

When it comes to making calls to a Web Service in Silverlight you have no other way, but to do it async. In many scenarios this could be a problem and especially when you use data binding.

The Problem

Consider you have a business objects Merchant and Sale and the Merchant has property Sales of type List as shown on the class diagram.

 class_diagram

The sales are loaded from a web service.

public List Sales
{
    get
    {
        if ( this.sales == null )
        {
            this.sales = new List();
            MerchantServiceSoapClient client = new MerchantServiceSoapClient();
            client.GetMerchantSalesCompleted += new EventHandler( this.GetMerchantSalesCompleted );
            client.GetMerchantSalesAsync();
        }
 
        return this.sales;
    }
}
 
private void GetMerchantSalesCompleted( object sender, GetMerchantSalesCompletedEventArgs e )
{
    this.sales = e.Result;
}

Basically what you need in most cases is just to bind to the sales list. Ok, what do you think would happen if we use the following code?

Code
this.DataContext = new Merchant();
 
XAML
<ListBox x:Name="Sales" ItemsSource="{Binding Sales}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ProductName}"   />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The correct answer is nothing. No data will be displayed in the ListBox. The reason why this is true is simple - the ListBox is correctly bound to the Sales property, but it returns though null value because in the time of data binding there is no result returned from the web service.

The Solution

In order to make that works we need to implement the INotifyPropertyChanged interface and raise the PropertyChanged event once the data is received.

inotifypropertychanged

private void GetMerchantSalesCompleted( object sender, GetMerchantSalesCompletedEventArgs e )
{
    this.sales = e.Result;
 
    if ( this.PropertyChanged != null )
    {
        this.PropertyChanged( this, new PropertyChangedEventArgs( "Sales" ) );
    }
}

Now we are done - the ListBox is notified about the changed data and displays the sales correctly.

Summary

Going async has some gotchas on the way and you should be careful with them. That one is pretty common and developers, not used to using async operations, get surprised when they get no results. I'll be glad to give you answers to any other questions in this direction. Just post them as a comment.

Share


Comments

Comments RSS RSS
  • RE: Tip: Binding and getting data async from a Web Service  

    posted by Nick on Aug 13, 2008 01:52

    Good post, simple and clear. Thanx.

Add Comment

 
 

   
  
  
   
Please add 5 and 4 and type the answer here:

Join the free SilverlightShow webcast 'Running Silverlight Outside the Browser and with Elevated Trust'. Sept 7th, 8 am - 9 am PDT.
In this live session Chris Anderson will cover configuring and debugging OOB mode, toast notifications, elevated trust, direct file access and much more.
Learn more | Register | See more webinars (hide this)