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:

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:
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Data.Services.Client;
- using System.Linq;
- using System.Linq.Expressions;
- using GalaSoft.MvvmLight;
- using PivotViewerSeries.Silverlight.NetflixOData;
-
- namespace PivotViewerSeries.Silverlight.ViewModel
- {
- public class MainViewModel : ViewModelBase
- {
- private Uri _DataUri
- = new Uri("http://odata.netflix.com/Catalog/");
-
- private IEnumerable<Title> _Titles;
- public IEnumerable<Title> Titles
- {
- get
- {
- if (_Titles == null)
- return new ObservableCollection<Title>();
- return _Titles;
- }
- set
- {
- if (_Titles == value)
- return;
- _Titles = value;
- RaisePropertyChanged("Titles");
- }
- }
-
- public MainViewModel()
- {
- var MyContext = new NetflixCatalog(_DataUri);
- var MyData = new DataServiceCollection<Title>(MyContext);
- MyData.LoadCompleted += (o1, e1) =>
- {
- // This block of code runs when we get results.
-
- if (e1.Error != null)
- throw e1.Error;
-
- if (MyData.Continuation != null)
- {
- MyData.LoadNextPartialSetAsync();
- Titles = MyData;
- }
- else
- Titles = MyData;
- };
- var MyQuery = from c in MyContext.Titles.Take(1000)
- select c;
- MyData.LoadAsync(MyQuery);
- }
- }
- }
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:
- <UserControl x:Class="PivotViewerSeries.Silverlight.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:pivot="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="400"
- DataContext="{Binding Main, Source={StaticResource Locator}}">
-
- <Grid x:Name="LayoutRoot" Background="White">
- <pivot:PivotViewer ItemsSource="{Binding Titles}">
-
- </pivot:PivotViewer>
- </Grid>
- </UserControl>
Conclusion
When we run our new application, we should get something like the following:

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