Semantic Zoom with MVVMLight – Windows 8 Metro

This week I’ve been trying out some Windows 8 Metro development and by doing so I came across something I think will help out other devs.
I wanted to try out one of the key new features that is presented in Windows 8, called Semantic Zoom ( read about/view it here ).

But because of the fact I’m mostly familiar with Windows Phone 7 development, I wanted to use Laurent Bugnion’s MVVMLight framework also on Windows 8. You can get a v4Beta1 version here…

So by doing so, I needed to tweak my code so it would fit nicely with the Semantic Zoom pattern. Let me show you how I did this.

First create a new MVVMLight Project

image

Than open the Model folder and add a class called Car.cs

 

And a class called Manufacturer.cs

These 2 classes will define your Group and Items needed for the Semantic Zoom. So for the Zoomed In view we will be presenting all Cars that we have ( grouped by manufacturer ) and in the Zoomed Out view we will show a birds eye overview of all Manufacturers with some general details about the amount of Cars available.

Next thing on our list is adding an ObservableCollection of Manufacturers to the MainViewModel.

Be sure to initiate your data, we will now for this demo do this in the MainViewModel constructor.

 

Now open up your MainPage.xaml and get rid of the following TextBlock

Put following code into place ( where the textblock was previously set ) to get your title and backbutton

So now we need to add our Semantic Zoom control, do this directly underneath the backbutton and page title xaml

 

Before we continue we will be adding a CollectionViewSource, this is needed to fill the Item grid later on in the Semantic Zoom control! You place this inside the MainPage.xaml <ResourceDictionary> tag, it should be there already and contain <ResourceDictionary.MergedDictionaries> tag… Take note that it is actually here that we will be binding to our ObservableCollection froml our MainViewModel!

 

So with this in place we can continue with our ZoomedIn view, all we will be doing is defining visual elements on how we want to structure our data. In our case we will be adding the Manifacturer name as Group header with Car models underneath it, to accomplish this add following xaml inside the <SemanticZoom.ZoomedInView></SemanticZoom.ZoomedInView> tags

 

Now that the ZoomInView is ready, add following xaml for our ZoomOutView. Now we will again show the Manufacturer name but only with a total Car count underneath it so we can get our birds eye overview! Place the code inside the <SemanticZoom.ZoomedOutView></SemanticZoom.ZoomedOutView> tags

 

Before we are done we do actually need to add one line of extra code in the MainPage.xaml.cs file! To get the ZoomedOutView working we need to link the groupGridView with the groupedItemsViewSource

So inside the constructor of the MainPage.xaml.cs add following line underneath the this.InitializeComponent(); call

And that’s it… if everything goes well you should have following result

image

image

The complete solution can be downloaded here… MvvmLight1

Tweetsharp and AgFx – Windows Phone 7

Being a big fan of AgFx for any online data retrieval, I was exited to see Bjorn Kuiper his blog post about using Tweetsharp together with AgFx in a WP7 app. You can read all about it here… it shows you how to display tweets from a specific users’ timeline.

But like many blog posts, most of the time you’ll have to tweak them to work in your own scenario. So also with this one, I needed to manipulate some code to get my case working. Best to start off, is downloading Bjorn’s example here.

What I wanted to achieve, was to show a list of tweets done by a Search, also through the use of Tweetsharp and AgFx of course. But what I soon found out was that the code of Bjorn uses the class TwitterStatus that contains a tweet, in my case the returning items are of type TwitterSearchStatus. They are actually not that different, but I ran into some serialization/deserializations issues.

So what was my problem… when you get your search results back from Tweetsharp you want to serialize them so that you can use the AgFx feature. In Bjorn’s example, he strips the Entities from the TwitterStatus before serializing. Found in the Execute method of the TwitterLoadRequest class.

 

But like I stated before I’m not working with TwitterStatus but with TwitterSearchStatus and it seems that performing the same action has no effect and I was still getting problems with the Entities property when Deserializing.

So how did I solve this, well I used Json.net from James Newton-King, also available as a NuGet here. So, instead of using the .Net DataContractJsonSerializer like Bjorn, we use the JsonConvert.SerializeObject. This gives us following code that replaces the one show above:

What you will notice is that I’m doing a special kind of Serialization… using a DynamicContractResolver! This is needed to manipulate the Json serialization, because if we don’t do anything, we’ll still have an output Json string containing the Entities properties.

So to fix this, you’ll need to interact when Json.Net tries to convert your class to Json, how to do this is is by creating your own class that inherits from DefaultContractResolver.

 

As you can see, Json.net finds properties to serialize, so he will pass the CreateProperties method, what we do here is use Linq to filter out the Entities, resulting in a total Json outpot string containing serialized TwitterSearchStatus objects without any Entities property.

The only thing left to do is Deserialize everything back in the Deserialize method of your AgFx DataLoader class.

 

So now you’ll be getting TwitterSearchStatus objects back from AgFx that you can use in your WP7 Views! Happy coding…