Tuesday, August 2, 2011

Bringing F# to the Microsoft Surface Table

Within the last month or so Microsoft has dropped some really cool
SDK's for the public to try out. One of them is the Microsoft -
Surface 2.0 SDK & Runtime available here. Once you download
and install the tools and runtime you'll notice a few new applications
in your start menu. Input Simulator and Input Visualizer. These two
apps are designed to help you interact with your custom surface
application if your dev box is not touch enabled. Now that we
have the basics its a good time to figure out what you're going to build
to get familiar with the surface API's. Luckily, we have a few things
to help us get started. Existing WPF/Silverlight skills and a little sample
documentation. Anytime I start hacking against a new API I always look
for some sample code or quick start to get me going. Unfortunately, the
sample code at the link above has no support for F#. However we know
better. We know that anything C# or VB can do F# can do better. :-)
So let's fire up VS2010 and create an new F# console application using
the console application template. Once you've done that you'll need to
add the following references to your project. These references will make
the console application wpf & surface enabled.


Now to tip our toes in the Surface SDK water. We'll start by creating a
simple Scatter View application which basically scatters a bunch of
images on the screen for the user to interact with. This is also the
recommended "first application" in the above documentation.

open System
open System.IO
open System.Windows.Media.Imaging
open System.Windows
open System.Windows.Controls
open Microsoft.Surface
open Microsoft.Surface.Presentation.Controls

let app = new Application()
let window =
     new SurfaceWindow(Title= "F# ScatterView Surface Demo",
                                                      AutoOrientsOnStartup = true)
(* scatter view control bound to windows folder - my pictures*)
let path = @"C:\Users\Public\Pictures\Sample Pictures"
let scatter = new ScatterView()
scatter.ItemsSource <-
        seq {
                 for f in Directory.GetFiles(path, "*.jpg") do
                     yield new Image(Source=
                                         new BitmapImage(Uri(f, UriKind.Relative)))

window.Content <- scatter
[] ignore <| (app).Run window

As you can see from the code above the API is very similar to
WPF/Silverlight, their is however a gotcha that I experienced when
playing around with the API (which you may be able to see).

When creating a SurfaceWindow you must have an application instance
already created otherwise you can not create an instance of a
Surface Window. :-(
Other than that it is really just a matter of new'ing up controls specific
to surface and placing them on the window or user control as normal.
Also, it's worth mentioning that you can do all this in Xaml and still have
your logic in F#. :-)

-Sample screen shot-

Until next time...
-Develop with passion

No comments:

Post a Comment