Embedding a F# class library in a Silverlight 2 Application
I’ve been preparing for my F# session at the MSDN Developer Conference in Orlando on December 11 and came across a blog post on creating a Silverlight application using F#. Here is the original post:
http://jyliao.blogspot.com/2008/11/f-and-silverlight-20.html
Unfortunately it was only a description of how to do it, but there wasn’t a sample. In this post, F# was used to do everything. This isn’t really what I was looking for. I wanted to write a C# Silverlight application but call into an F# class library.
Disclaimer: This is a hack and I’m not responsible for what it might do to your system. Also I’m not sure of the licensing of the FSharp.Core.dll but I would guess that you can’t redistribute it, so only use it for testing until a true FSharp.Core.dll is available for Silverlight.
For those who aren’t familiar with F#, it’s a functional or declarative language that compiles into .NET byte code and is becoming popular for banking and scientific applications. You can read more about F# here:
http://msdn.microsoft.com/en-us/fsharp/default.aspx
Compiling F# for Silverlight is a bit tricky right now because a Silverlight version of the runtime hasn’t been released as far as I can tell, and there is no project wizard to create an F# Silverlight project.
After a lot of hacking, I’ve come up with something that will work, and is easy for anyone to start using. What I have created is a Visual Studio project template which creates a C# Silverlight application and also an F# class library.
FSharp.Core.dll is the F# runtime. Since it’s not compiled against the Silverlight runtime, can’t be added to a Silverlight project in Visual Studio, but if the project creation wizard puts it in there it seems to work ok.
The first thing you’ll need to do is install the F# 1.9.6.2 CTP
Then download this project template that I created:
http://www.bluerosegames.com/silverlightfsharpproject.zip
and put it in your Documents\Visual Studio 2008\Templates\ProjectTemplates folder. Once you do this, if you do a “New Project” in Visual Studio, you should see a new template under the “My Templates” section:
When you create this app, you’ll get some warnings about trusted imports, etc. If anyone can figure out how to get rid of these, please let me know.
What you’ll have after creating the project is a main Silverlight Application, a web application if you chose to create one, and a F# class library project. The F# class library would typically have a .fsproj extension, but I could only get it working if you use a .csproj extension, so there you go, told you it was a hack.
To get you started with a Hello World type sample app, the Module1.fs in the F# project is created with the following:
#light
namespace SLFSharpApp16_FSharp
type TestClass = class
new () as this = {}
member s.Hello() =
"Hello from F#"
end
This creates a class called TestClass with a method called Hello that returns a string.
Now in the main Silverlight project, the Page.xaml is generated with a TextBlock in it that we will populate with the text from the Hello method:
<UserControl x:Class="SLFSharpApp16.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="text" Text="Text from FSharp test will go here"/>
</Grid>
</UserControl>
and the Page.xaml.cs calls into the F# class library:
using SLFSharpApp16_FSharp;
namespace SLFSharpApp16
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
TestClass test = new TestClass();
text.Text = test.Hello();
}
}
}
If you run the solution without any changes after it’s created you should see the following, with the text coming from the F# Hello method:
