DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Consuming and Persisting State of OData in Windows Phone

Sumit Dutta user avatar by
Sumit Dutta
·
Jul. 04, 12 · Interview
Like (0)
Save
Tweet
Share
3.13K Views

Join the DZone community and get the full member experience.

Join For Free

In this article I will talk about consuming and persist the state of OData in Windows Phone. One can read about OData here. I will use northwind service provided by OData

Let's write code:

Step 1: Create a new Windows Phone Application project. Select Silverlight for Windows Phone installed templates.

Step 2: Add service reference of Nothwind.svc and name it Northwind

http://services.odata.org/Northwind/Northwind.svc/

Step 3: Place ListBox inside content panel of Grid in MainPage.Xaml

<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=CompanyName}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
            <TextBlock Text="{Binding Path=ContactName}" TextWrapping="NoWrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
            <TextBlock Text="{Binding Path=Phone}" TextWrapping="NoWrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

Step 4: Add below using directive in MainPage.Xaml.cs. Second using directive is Namespace of project and name of service reference.

using System.Data.Services.Client;
using WP_Consuming_OData.Northwind;

 Step 5: Define below class level variables.

public partial class MainPage : PhoneApplicationPage
{
   private NorthwindEntities context;
   private readonly Uri northwindUri =
   new Uri(http://services.odata.org/Northwind/Northwind.svc/);
   private DataServiceCollection<Customer> customers;
   private bool isDataLoaded;

 Step 6: Add below method in MainPage.Xaml.cs which will be invoked whenever application is restored. isDataLoaded variable to check if data is already in memory, if data is not in memory the State dictionary will be checked to load, if data is not present in State dictionary also the then OData service will be invoked.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
   // Get the object data if it is not still loaded.
   if (!this.isDataLoaded)
   {
      object storedState;
      DataServiceState state;
      if (this.State.TryGetValue("DataServiceState", out storedState))
      { 
         // Deserialize the DataServiceState object.
         state = DataServiceState.Deserialize(storedState as string);
         var restoredContext = (NorthwindEntities)state.Context;

         // Set the binding collections from the stored collection.
         var restoredCustomers = state.RootCollections["Customers"] as DataServiceCollection<Customer>;
      this.LoadData(restoredContext, restoredCustomers);
      }
      else
      {
         this.LoadData();
      }
   }
}

Step 7: Add below method in MainPage.Xaml.cs which will serialize and store data serive state.

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
   var collections = new Dictionary<string, object>();
   collections.Add("Customers", customers);
   this.State["DataServiceState"] = DataServiceState.Serialize(context, collections);
}

Step 8: Now add below LoadData method in MainPage.Xaml.cs which will be invoked when stored data context is used to bind.

public void LoadData(NorthwindEntities context, DataServiceCollection<Customer> customers)
{
   this.context = context;
   this.customers = customers;
   this.isDataLoaded = true;
}

Step 9: LoadData method in MainPage.Xaml.cs will be called to load the data from service first time and in case context is not stored in page state.

private void LoadData()
{
   context = new NorthwindEntities(northwindUri);
   customers = new DataServiceCollection<Customer>(context);
   var query = from cust in context.Customers select cust;
   customers.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(customers_LoadCompleted);
   // Load the Customers feed by executing the LINQ query.
   customers.LoadAsync(context.Customers);
}

Step 10: Add below methon in MainPage.Xaml.cs which occurs when loading of data is completed.

void customers_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
   if (e.Error == null)
   {
      if (customers.Continuation != null)
      {
         customers.LoadNextPartialSetAsync();
      }
      else
      {
         this.LayoutRoot.DataContext = customers;
         this.isDataLoaded = true;
      }
   }
   else
   {
      MessageBox.Show(string.Format("An error has occurred: {0}", e.Error.Message));
   }
}

Step 11: Now run the application and wait for data to load and bind to listbox and then press Start button.

You will get exception Northwind.NorthwindEntities cannot be serialized like below.

WP-Consuming OData

Now open Reference.cs available in below shown path.

Windows Phone - Consuming OData

Add below using directive in Reference.cs

using System.Runtime.Serialization

Now add [DataContract] attribute to NorthwindEntities class

[DataContract]
public partial class NorthwindEntities : global::System.Data.Services.Client.DataServiceContext
{

Step 12: Now run the application and wait for data to load and bind to listbox and then press Start button. This time there won't be any exception.

This ends the article of consuming and persisting state of OData in Windows Phone.

 

 

Windows Phone

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner’s Guide To Styling CSS Forms
  • What Is API-First?
  • Use AWS Controllers for Kubernetes To Deploy a Serverless Data Processing Solution With SQS, Lambda, and DynamoDB
  • Custom Validators in Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: