(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: Binding and getting data async from a Web Service

(5 votes)
Emil Stoychev
>
Emil Stoychev
Joined Oct 15, 2007
Articles:   23
Comments:   98
More Articles
3 comments   /   posted on Aug 12, 2008
Categories:   Data Access

This article is compatible with the latest version of Silverlight.


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.


Subscribe

Comments

  • -_-

    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.

  • -_-

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


    posted by Bart on Apr 11, 2011 13:31

    Great tip! We've been struggling with this for a while and the solution was so simple ! Many thx.

  • -_-

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


    posted by Jonah on May 25, 2011 23:57
    Thanks, that helped alot... been stewing over this for a few hours.

Add Comment

Login to comment:
  *      *       
Login with Facebook