Windows Phone WCF OData Client

Windows Phone Tutorial

In a recent posting I showed how to create a quick OData service using Entity-Framework Code-First and SQLCE4.  In this posting I’ll show how to create a simple Windows Phone client to interact with that service.

Begin by creating the client, using the Windows Phone Databound Application template. Once the directory is created, you need to create a proxy for the server.  Open a command window and navigate to the directory for your application. 

Use the latest DataSvcUtil.exe from http://odata.codeplex.com.

Here are the options to use

/uri – This will point to the service URI. In this case, it’s http://localhost:?????/BookDataService.svc

/out – This is the DataServiceContext class that will be generated.

/Version – should be set to 2.0

/DataServiceCollection – Include this flag to generate collections derived from the DataServiceCollection base.

I  ran it as follows:

>L:\Downloads\OData\DataSvcUtil 
/uri:http://localhost:57361/BookDataService.svc 
/Version:2.0 
/DataServiceCollection 
/out:BookDataService.cs

 

Add the resulting file (BookDataService.cs) to the project, using Add->Existing. 

Modify the Xaml in your new client application to bind as you would bind to any enumerable source; that is begin by changing the binding on the ItemsSource property of the ListBox from Binding Item to just Binding

Keeping things simple, modify the Binding for LineOne and LineTwo to two of the public properties in your DataService.  Here’s the Xaml I created in the client,

<Grid
   x:Name="ContentPanel"
   Grid.Row="1"
   Margin="12,0,12,0">
   <ListBox
      x:Name="MainListBox"
      Margin="0,0,-12,0"
      ItemsSource="{Binding}"
      SelectionChanged="MainListBox_SelectionChanged">
      <ListBox.ItemTemplate>
         <DataTemplate>
            <StackPanel
               Margin="0,0,0,17"
               Width="432"
               Height="78">
               <TextBlock
                  Text="{Binding Title}"
                  TextWrapping="Wrap"
                  Style="{StaticResource PhoneTextSubtleStyle}" />
               <TextBlock
                  Text="{Binding ISBN}"
                  TextWrapping="Wrap"
                  Margin="12,-6,12,0"
                  Style="{StaticResource PhoneTextSubtleStyle}" />
            </StackPanel>
         </DataTemplate>
      </ListBox.ItemTemplate>
   </ListBox>
</Grid>

Add a member variable to hold the context from the Entity Framework. The db type is Book and the context is BookContext.  Initialize it using the URL of the service,

BookContext context = 
    new BookContext( 
        new Uri( 
            "http://localhost:57361/BookDataService.svc/" ) );

Create a second member variable of type DataServiceCollection<Person>  — note you will need to add a reference to System.Data.Services.Client.  Do not initialize this member until the constructor:

public MainPage()
{
    InitializeComponent();
    books = new DataServiceCollection<Book>( context );
    books.LoadAsync( 
        new Uri( 
            "http://localhost:57361/BookDataService.svc/Books" ) );

Finally, be sure to point the data context to the books collection. 

With this you have a client running against the service you created from  the earlier post.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Data, Essentials, Mango, Mini-Tutorial, Patterns & Skills, WindowsPhone and tagged . Bookmark the permalink.

One Response to Windows Phone WCF OData Client

Comments are closed.