Consuming RESTful Web Services in WPF

It turns out that consuming the RESTful web service created in my last post is very simple when WPF’s XML data binding features are used. I have previously posted about binding to XML data in a WPF application.

My RESTful web service uses XML namespaces in its return data, so I first declare an XmlNamespaceMappingCollection in my WPF application XAML. Next I declare an XmlDataProvider for the composer list and set its Source property to the URI of my Composers RESTful operation. A second XmlDataProvider is declared for the composition data, but that is still empty at this time.

<Window.Resources>
    <XmlNamespaceMappingCollection x:Key="NamespaceMapping">
        <XmlNamespaceMapping Uri="https://patconroy.wordpress.com/data" Prefix="pc" />
    </XmlNamespaceMappingCollection>
    <XmlDataProvider x:Key="ComposerData" 
                     XmlNamespaceManager="{StaticResource NamespaceMapping}"
                     Source="http://localhost:8000/service/restbaroque/composers"/>
    <XmlDataProvider x:Key="CompositionData"
                     XmlNamespaceManager="{StaticResource NamespaceMapping}"/>
</Window.Resources>

A ListBox created on the main Window that binds to the ComposerData XML source. Note that the StringFormat property used on the MultiBinding here was introduced in the 3.5 SP1 version of the framework. It is not available in earlier versions.

<ListBox x:Name="_composerList" Margin="40,20" 
                 ItemsSource="{Binding Source={StaticResource ComposerData}, XPath=//pc:Composers/pc:Composer}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} {1} {2}">
                        <Binding XPath="pc:FirstName"/>
                        <Binding XPath="pc:MiddleName"/>
                        <Binding XPath="pc:LastName"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

That’s all the work the needs to be done to display the composer list. A Click event handler for a button on the Window sets the appropriate Source for the compositions XmlDataSource.

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (_composerList.SelectedItem == null)
    {
        MessageBox.Show("Please select a composer");
    }
    else
    {
        XmlNamespaceManager ns = Resources["NamespaceMapping"] as XmlNamespaceManager;
        XmlNode node = _composerList.SelectedItem as XmlNode;
        string id = node.SelectSingleNode("pc:Id", ns).InnerText;
        string uriString = String.Format("http://localhost:8000/service/restbaroque/composers/{0}/compositions", id);
        Uri uri = new Uri(uriString);
        XmlDataProvider dp = Resources["CompositionData"] as XmlDataProvider;
        dp.Source = uri;
    }
}
 

A second ListBox on the Window binds to the compositions XmlDataProvider.

<ListBox x:Name="_compositionList" Margin="40,20" 
         ItemsSource="{Binding Source={StaticResource CompositionData}, XPath=//pc:Compositions/pc:Composition}"/>

That’s it!
restfulclient

Advertisements

0 Responses to “Consuming RESTful Web Services in WPF”



  1. Leave a Comment

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




March 2009
M T W T F S S
« Feb   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
I am a part of all that I have met;
Yet all exprience is an arch whitherthro'
Gleams that untravell'd world, whose margin fades
For ever and for ever when I move.
How dull it is to pause, to make an end,
To rust unburnish'd, not to shine in use!
Alfred, Lord Tennyson

%d bloggers like this: