EventBrite and AgFx – Windows Phone development

So you will already know that anything linked to online data should be retrieved using AgFx! Now if you have that, let me show you how you can use this for retrieving event data from EventBrite ( an online service to host an event calendar and maintain attendees data ).

EventBrite is a cool online service that also comes packed with a great API that will allow devs to integrate it in their own apps.

Before I continue I must point out that there is also a clean .net wrapper available, called EventBrite.Net but like I stated above, I’m more hooked on AgFx so I didn’t used the wrapper.

Before you can do anything with the EventBrite API you first should get an API key through their website here… After that you are set to go! With that API key you can than start using all API methods that are described on the EventBrite API documentation here…

For this demo we will try to download a list of events for a given organizer, the API method to get this info is organizer_list_events. But before we can do anything in our program we need to have .net objects representing the EventBrite response. Getting this information is not that difficult… First find an organizer and get it’s ID. You’ll only need to get the list of all events given by the organizer and EventBrite will show you his ID in the URL: I used this one http://www.eventbrite.com/org/2276332563 so the ID here is 2276332563

With this, you can use Fiddler to take a look at the returning JSON result. Open the composer tab in Fiddler and paste a legal EventBrite API method URL in the GET input box: https://www.eventbrite.com/json/organizer_list_events?app_key=[YOUR KEY HERE]&id=2276332563
When everything is ok, you’ll get a big JSON result back that contains all event information. This result will help you getting the needed .net objects! In Fiddler open the Raw tab in the Response pane and only copy the JSON string… after you have copied that, use a word processing tool to strip it of all unneeded line returns ( ex: Notepad++ with TextFX plugin – Unwrap text ). With your currently stripped JSON response go to the fantastic site called json2csharp! Paste your JSON code and press the Generate button, when all goes well you’ll get following result:

Or you can just copy it from my blog post Winking smile

DO NOTE that not it seems that not all JSON names are .net compatible resulting in _invalid_name_ prefixes! My suggestion is to change the .net class and ‘manipulate’ the JSON mapping in your app! ( I’m NOT covering this in the rest of the example )

So you added these classes in some Model part of your project, next up hooking up AgFx to EventBrite. To get this working you’ll need to create an AgFx ‘viewmodel’, here is how I did it:

A lot of code, but if you are used to working with AgFx, most of it will look familiar… The only ‘special’ thing is being done inside the GetLoadRequest method, here we need to give AgFx our API method URL. So I just wrapped all possible methods inside a static class EventBriteAPI. But in the end all it does is generate the same URL we used in Fiddler being: https://www.eventbrite.com/json/organizer_list_events?app_key=[YOUR KEY HERE]&id=2276332563

So that’s it, now you will have all events retrieved from EventBrite through AgFx! Happy coding!

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…

LongListSelector – Windows Phone 7 development

If you are using a Windows Phone 7, you’ll have noticed that when you want to go through your People list they are all ‘grouped’ by one character. And when you press one of those characters, you’ll get a nice overview of all available groups, again shown by a character.
Example example

Now if you are a wp7 developer and want to have the same kind of behaviour for any kind of ‘lists’ you want to present to the enduser, you have 2 options, 1. develop your own control or 2. use the LongListSelector available in the Sliverlight toolkit for WindowsPhone7!

Now to use this control, you better read up 2 posts available on windowsphonegeek

Now if you follow these posts, you’ll notice you won’t get the same look and feel as on the People list in the Windows Phone itself… now to overcome this, you’ll better read up Benjii’s post on how to use the LongListSelector!
Because when you follow his instructions, you’ll get a better UI in reference to the People list on the Windows Phone.
But somehow when I used his solution, I was not able to get a correct Item Selection Change event to Command working. Somehow Benjii’s group class won’t let me cast back to one selected item in the ViewModel.

So again I needed a solution! And after some twitter chatter and a question on stackoverflow, I was directed to a custom implementation done by Claus Jørgensen!
The best way to look at his grouping class and LongListSelector usage, is to download his weather app from GitHub!
It contains a fine example on how to use the LongListSelector and also an extension that is needed to get Command binding to the SelectionChanged event!
His Grouping class also makes it possible to get the correct item from when the selection changed event get’s fired!

I did however change one small detail of his Grouping class ( LongListCollection.cs ), because when you do nothing with Claus’ class, you’ll only see the item group keys of the available items. And Benjii’s solution, first add’s dummy key/list items so that you’ll get the same overview as on the People list, when a group has no items the group key will be greyed out!!

To get this working with Claus’ class, I added a DefaultHeaders list! Here is my implementation of the class:

Now if you want to use this in your ViewModel, you only need to add the following 2 lines:

I hope you guys have as much fun with the LongListSelector as I did 😉

Windows Phone 7 development

Currently I released 2 Windows Phone 7 apps to the Microsoft Marktplace. The creation of both apps was not that difficult, because when you want to develop for wp7, there are several good reusable libraries out there!

Let me zoom in on one in particular, called WP7Contrib! It’s a wp7 library that is available on CodePlex, so the sourcecode is also available! But why am I using it, well let me give you some examples.

* Tombstoning
Each time your wp7 app gets tombstoned, there is a great chance you’ll want to ‘save state’, this helps the user experience of your app. There are several good examples out there that handle this, but I like the implementation of it in wp7contrib.
Basically you’ll create a new ViewModelBase class that implements 2 methods: IsBeingActivated(IStorage storage); and IsBeingDeactivated(IStorage storage); Now when you create a ViewModel, you’ll inherit from this base class, and because wp7contrib has a StorageService that gets passed along as parameter, you can use it to Write and Read data to/from it!
So each time an app gets tombstonded the IsBeingDeactivated method is called on each instantiated ViewModel, enabling you to write code like this: storage.Write(“zoomLevel”, this.ZoomLevel); This example will write the current value of a local property to the StorageService and when the app resumes you can put following code in the IsBeingActivated method: this.ZoomLevel = storage.Read(“zoomLevel”);
And presto, your View will again contain the data the user had put in before tombstoning!

* Isolated Storage
A similar feature is a nice wrapper around the Isolated Storage called StoreSettings. It gives you some method calls that are easy to use! Some examples:
– this.SettingsService.Write(“UserName”, this.UserName); This will store a given View textbox to the Isolated Storage
– this.SettingsService.TryToRead(“UserName”, out userName); This will try to retrieve the value back again, it will return True if the Key was found in the Isolated Storage

* Page navigation
Page navigation from ViewModels in a MVVM app is something that needs to be addressed correctly! Because when you want to submit an app to the marktplace, this is something that has to be working 100% and there are even some rules applied to it.
My tip, just use the NavigationService available in wp7contrib! If you want to ‘navigate’ to another view from your viewmodel, you’ll only have to add following code: this.SettingsCommand = new RelayCommand(() => this.NavigationService.Navigate(ViewModelLocator.SettingsPageUri));
Side note here, I’m setting up a RelayCommand in the constructor of the ViewModel, so that I can trigger this from an appbarbutton in the View!

* Bing Maps location
One last item that I’m a huge fan of in this wp7contrib lib, is the LocationService! One of my apps has Bing Maps integrated and when you need to get a current location fix ( or you want to track location for a period of time ), again you’ll have to write a lot of code to get it done ‘right’.
But with wp7contrib you’ll only need to call the correct methods and everything will be handled for you!
In my app I current allow the user to get his current location fix from the push of an appbar button, with this I’m triggering following code:
_currentSubscriber = _locationService.Location()
.ObserveOnDispatcher()
.Subscribe(location => this.CurrentPosition = location);

What it does, is fire up the location service and return the current phone location if it is available! When I get it, I’ll just set the CurrentPosition, what in my app is a MVVM binded property to a pushin on the BingMaps control in my view… and presto the current location is visible!

There are also some cool controls available and the one I like most is the SmartTextBlock, just take a look here for the implementation.

Well the list could go on, but currently these 4 are my mostly used ones! I just have to say that wp7contrib helped me speed up my wp7 app development and I would suggest you’ll check it out.
It is available on the wp7contrib codeplex site and the main page contains a good overview of most of the features.