Inside F#

Brian's thoughts on F# and .NET

F# for Silverlight 4 available

Posted by Brian on May 17, 2010

Today the final Silverlight 4 Tools for Visual Studio 2010 were released (go here for download link).  These tools include the F# runtime (FSharp.Core.dll) for the Silverlight 4 runtime.  For those who may have previously been held up developing with F# for Silverlight 4, today is the day to get unblocked!

To commemorate the occasion, I made a tiny F# ‘hello world’ Silverlight application in the traditional fashion (a C# app with an F# library).  I’ll walk you through the steps.

(Ensure you have already installed the final version of Silverlight 4 tools for VS2010.)

In VS, go to the ‘New Project’ dialog and select the ‘F# Silverlight Library’ template

FS_SL_Library

Then when it asks to choose a Silverlight version, pick Silverlight 4:

FS_SL_PickVersion

Then, for the purposes of this example, I replaced the code in Module1.fs in the new project with this code:

namespace MyFSharp

type MyType() =
    static member FilterOutZs (strs:seq<string>) =
        seq { for s in strs do
                if not(s.StartsWith("Z")) then
                    yield s }

We’ll see how I’ll use this code shortly.

Next, right click on the solution in Solution Explorer and ‘Add… New Project’ a ‘C# Silverlight Application’:

CS_SL_App

Then it will pop up a dialog about hosting the new Silverlight app, I choose to uncheck the ‘Host the Silverlight application in a new web site’ box.  Once again, be sure that ‘Silverlight 4’ is selected as the Silverlight version in the dialog.

CS_SL_Dialog

Next I added the highlighted bit to the MainPage.xaml in the C# app:

CS_SL_Xaml

and then in the C# code-behind, MainPage.xaml.cs, I had this handler:

private void TheText_MouseEnter(object sender, MouseEventArgs e)
{
    this.TheText.Text =
        string.Join(" ", MyFSharp.MyType.FilterOutZs(
            new[] { "ZZZ", "Hello", "ZZZ", "from", "F#", "ZZZ" }).ToArray());
}

which calls my F# code.  To make this compile, I need to add a project reference, so right click on the C# project, ‘Add Reference…’ select the ‘Projects’ tab in the dialog, and select the F# library from your solution. 

Right click the C# app in Solution Explorer and select ‘Set as StartUp Project’.  (If I’d created the app first, and then added the library, rather than the other way around, I wouldn’t need this step.)

Now I can press F5 to run it, and I see in my browser:

HelloFromFS

Not the most enthralling app ever, but it shows that F# is working with Silverlight 4.  Of course you already know how to make more exciting F# Silverlight apps.

Have fun enjoying Silverlight 4 with F#!

Advertisements

2 Responses to “F# for Silverlight 4 available”

  1. Cameron said

    Awesome! Now we need a F# runtime for Windows Phone Silverlight.

  2. Brian said

    And speaking of F# and Silverlight, the F# PowerPack has been updated with some bug fixes, and now with Silverlight DLLs. http://fsharppowerpack.codeplex.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: