I recently had to do some travelling for the VS launch event in Las Vegas and QCON event in Beijing. One of the things thats nice about travelling is that it gives you time to do some things you may not otherwise do - in my case, I had a chance to try to (re) learn WPF and see how I can use F# with WPF. This sample is a result of that.
This sample builds on the F# Twitter streaming sample that Don blogged about. Our goal will be to take the sample and modify it to build a WPF UI on top of the twitter event stream so that we can visualize the tweets.
The overall implementation strategy is to implement an event handler for the twitter events and push updates into an observable collection, which is a WPF collection that can be data-bound to a UI control. XAML data binding is used to hook up the elements of the collection to the list box items.
To start off, we have code loads the XAML file (which was compiled as a resource into the F# application) and then finds the controls we are interested in:
Next, we create an observable collection, bind it to the image list, and pass that to a function which sets up the event handler:
Here’s how we setup the event handler:
Notice that the event handler will eventually insert the parsed tweet into the observable collection. (If you are not familiar with events in F#, I will post a blog entry on this in the near future).
The parseTweet function takes the XML and returns UseStatus type:
Once we data bind the “root” object via F# code, the XAML code takes care of binding elements of the observable collection (which have type UserStatus). Here’s the interesting XAML code in the data template for the ListBox that binds the elements of the collection to the list box items:
Notice that the image is bound to the URL specified in the ProfileImage field of the UserStatus type, and the Label content is bound to the Status field of the UserStatus type.
I hope this sample highlights how to integrate F# and F# events with WPF.