PivotViewer V2 Series, Part 3: Receiving OData

by JasonRShaver 31. October 2011 17:19

Other articles in the PivotViewer V2 Series:

  • Part 1: Intro
  • Part 2: Creating a Project
  • Part 3: Receiving OData
  • Part 4: Tradecards

Connecting To an OData Collection

Lets get some data for PivotViewer to play with.  The easiest protocol for getting data for PivotViewer currently is OData.  This, by far, not the only way to consume data, but PivotViewer is most useful with large data collections and OData is a good way to navigate those feeds (when available). 

A good feed to play with for now is the Netflix OData Catalog API.

Right click on the PivotViewerSeries.Silverlight project and select Add Service Reference:

image

Connecting ViewModel to Data

To get the data from the ‘cloud’ to our PivotViewer, we need to make a stop at the ViewModel first.  Load up ViewModel\MainViewModel.cs and add the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Data.Services.Client;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using GalaSoft.MvvmLight;
  8. using PivotViewerSeries.Silverlight.NetflixOData;
  9.  
  10. namespace PivotViewerSeries.Silverlight.ViewModel
  11. {
  12.     public class MainViewModel : ViewModelBase
  13.     {
  14.         private Uri _DataUri
  15.             = new Uri("http://odata.netflix.com/Catalog/");
  16.  
  17.         private IEnumerable<Title> _Titles;
  18.         public IEnumerable<Title> Titles
  19.         {
  20.             get
  21.             {
  22.                 if (_Titles == null)
  23.                     return new ObservableCollection<Title>();
  24.                 return _Titles;
  25.             }
  26.             set
  27.             {
  28.                 if (_Titles == value)
  29.                     return;
  30.                 _Titles = value;
  31.                 RaisePropertyChanged("Titles");
  32.             }
  33.         }
  34.  
  35.         public MainViewModel()
  36.         {
  37.             var MyContext = new NetflixCatalog(_DataUri);
  38.             var MyData = new DataServiceCollection<Title>(MyContext);
  39.             MyData.LoadCompleted += (o1, e1) =>
  40.             {
  41.                 // This block of code runs when we get results.
  42.  
  43.                 if (e1.Error != null)
  44.                     throw e1.Error;
  45.  
  46.                 if (MyData.Continuation != null)
  47.                 {
  48.                     MyData.LoadNextPartialSetAsync();
  49.                     Titles = MyData;
  50.                 }
  51.                 else
  52.                     Titles = MyData;
  53.             };
  54.             var MyQuery = from c in MyContext.Titles.Take(1000)
  55.                           select c;
  56.             MyData.LoadAsync(MyQuery);
  57.         }
  58.     }
  59. }

This block of code create a collection to hold our list of NetFlix titles.  Then, when our ViewModel is created, we asynchronously load our collection of data via LINQ.

Of special note here is that the DataServiceCollection will automatically page the collection in groups of 500.  When that happens, we get a chance to process the current incomplete set, and call LoadNextPartialSetAsync to get the next set of results. 

Connecting PivotViewer to ViewModel

The last step here is to attached our list of titles to our PivotViewer.  This is much easier to do now with PivotViewer 2.0.  We just have to bind to the ItemsSource property:

  1. <UserControl x:Class="PivotViewerSeries.Silverlight.MainPage"
  2.    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.    xmlns:pivot="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot"
  7.    mc:Ignorable="d"
  8.    d:DesignHeight="300" d:DesignWidth="400"
  9.    DataContext="{Binding Main, Source={StaticResource Locator}}">
  10.  
  11.     <Grid x:Name="LayoutRoot" Background="White">
  12.         <pivot:PivotViewer ItemsSource="{Binding Titles}">
  13.          
  14.         </pivot:PivotViewer>
  15.     </Grid>
  16. </UserControl>

 

Conclusion

When we run our new application, we should get something like the following:

image

What we are seeing is 1000 NetFlix titles.  Pretty impressive huh?  What, you don’t believe me.  Well, join me on the next step to get some visuals.

Part 4: Tradecards

Tags: , , ,

Blog

About the author

I am a software developer working for Microsoft in Redmond, WA.  In addition, my wife and I own TTXOnline, what is likely the 3rd largest table tennis store in the US.

Month List

Page List