Using oData Services in a Windows Phone Application

Introduction

Windows Phone application developers will be excited to hear that the platform has support for OData - the Open Data Protocol championed by Microsoft. In an earlier article, we learned about the OData Protocol. The CodePlex site for OData http://odata.codeplex.com/ provides many client libraries to allow .NET developers to consume oData data services in their applications.

One such library that the Codeplex site offers is the client library for the Windows Phone platform.

This client library can be downloaded from http://odata.codeplex.com/releases/view/54698#DownloadId=161665

The client library contains System.Data.Services.Client.dll, System.Data.Services.Design.dll and DataSvcUtil.exe, among other files.

Let us use the client library to create a working Windows Phone application that uses an oData data service.

Hands-on

Let's create an application that lists all of the available languages in which Netflix offers movies.

To begin with, create a data class for the Netflix oData data service.

The URI of Netflix oData data service is http://odata.netflix.com/v1/Catalog/

You create the data classes using DataSvcUtil.exe.

datasvcutil /uri:http://odata.netflix.com/v1/Catalog/ /out:.\Netflix.cs /Version:2.0 /DataServiceCollection 

This will create the Netflix.cs data class.

Now create a new Silverlight for Windows Phone application called WindowsPhoneApplicationODataDemo and add a reference to the System.Data.Services.Client.dll. Also add the Netflix.cs to the project.

Next, modify the MainPage.xaml to add a ListBox, which will display the listings of the languages Netflix offers. The modified xaml will look like this:

<!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
 
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Netflix Offers" 
                        Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Customers" Margin="9,-7,0,0" 
                        Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432">
                            <TextBlock Text="{Binding Path=Name}" TextWrapping="NoWrap" 
                                       Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        </StackPanel>
                    </DataTemplate>
            </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

Next, create an event handler for the MainPage's PageLoaded event. In this event hander, add the code to create a DataServiceContext and DataServiceCollection object.

Also, in this method, create an eventhandler for the DataServiceCollection's LoadCompleted event.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Initialize the context and the binding collection 
            netflix = new DataServiceContext(netflixUri);
            languages = new DataServiceCollection<Language>(netflix);
 
            // Register for the LoadCompleted event.
            languages.LoadCompleted
                += new EventHandler<LoadCompletedEventArgs>(languages_LoadCompleted);
 
            // Load the languages feed by using the URI.
            languages.LoadAsync(languagesFeed);
 
        }

Now, add the code in the languages_LoadCompleted to display the data returned from the data service.

void languages_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Handling for a paged data feed.
                if (languages.Continuation != null)
                {
                    // Automatically load the next page.
                    languages.LoadNextPartialSetAsync();
                }
                else
                {
                    this.LayoutRoot.DataContext = languages;
 
                }
            }
            else
            {
                MessageBox.Show(string.Format("error occured"));
            }
        }

Compile and run the application. You will notice that when the DataServiceCollection object is loaded, the application shows all the languages Netflix offers movies in.

The DataServiceCollection object is loaded
Figure 1: The DataServiceCollection object is loaded

If you scroll down, you will find more languages.

Summary

In this article, we saw how to create a Windows Phone application which consumes oData data services. Hope this article inspires you to write more data centric applications that utilize oData services.



About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds