Silverlight Brass Tacks

Bill Reiss' Silverlight Ramblings
My upcoming Silverlight book for beginners Hello! Silverlight 2 with Dave Campbell, available online now!



Pages

Recent posts

Navigation

Archive

Blogroll

Tampa Divorce Lawyer

North of Tampa in Lutz, Florida. A Tampa Divorce Lawyer focusing on family, divorce, and real estate law.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

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:

fsproj

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:

hellofs

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList
Posted: Dec 07 2008, 13:42 by Bill Reiss | Comments (23) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under:

Related posts

Comments

mgalinks.wordpress.com said:

pingbackPingback from mgalinks.wordpress.com

2008 December 08 - Links for today « My (almost) Daily Links

# December 07 2008, 19:57

Art Scott us said:

Art ScottThanks

# December 08 2008, 05:35

MichaelGG us said:

MichaelGGI found it works just fine by referencing the output of a properly compiled F# DLL (so long the output isn't in the "expected" place of bin\Debug or Release).

http://www.atrevido.net/blog/2008/10/28/F+1962+And+Silverlight+2.aspx

# December 08 2008, 11:04

Bill Reiss us said:

Bill ReissThanks Michael I'll try to incorporate this into the template.

# December 08 2008, 11:47

Tim us said:

TimHi! I've been trying to embed F# library using MichaelGG's method he described in the blog. Currently the code can build, and silverlight can reference the class also intellisense is available when using the class as well.

However, when I launch the silverlight website with F5, the error message keeps coming out.

"Failed to load pre-requisites.."

Any idea?

Thanks!

# December 18 2008, 21:40

Michael Giagnocavo us said:

Michael GiagnocavoTim, did you statically link the FSharp libraries in via --standalone?

# December 18 2008, 22:08

davidezordan.net said:

pingbackPingback from davidezordan.net

Embedding a F# class library in a Silverlight 2 Application | DavideZordan.net

# December 31 2008, 00:57

grahamsw us said:

grahamswMany thanks for this. I've been trying to get started using F# & Silverlight for data visualization. I can't tell you how much this helps.

# January 02 2009, 08:01

jammer ba said:

jammerMany thanks for this. I've been trying to get started using F# & Silverlight for data visualization. I can't tell you how much this helps.

# March 03 2009, 10:14

movers said:

moversTim, did you statically link the FSharp libraries in via --standalone?

# March 03 2009, 10:15

gmat said:

gmatu made me think alot
thanks for sharing

# March 03 2009, 10:16

Hectic Capiznon Bloggers 2009 ph said:

Hectic Capiznon Bloggers 2009thanks for sharing I will try this.

# March 12 2009, 17:14

Art Scott us said:

Art ScottThanks
? SL3 VS'10 F# ?

# March 19 2009, 07:41

Dallas Movers hr said:

Dallas Moversmade my day
great

# March 28 2009, 07:07

Atlanta Movers hr said:

Atlanta Moverslove it
thanks

# March 28 2009, 07:08

devix us said:

devixNice Article .....Thanks for sharing

# April 01 2009, 02:38

barackoli us said:

barackoliwhen i read this article....just can say it's useful for me.
Thank you

# April 12 2009, 17:00

movers us said:

moversthanks i like it, its good

# April 21 2009, 11:35

Austin Movers tr said:

Austin Moversgreat post
like it sir

# April 21 2009, 11:49

sohbet said:

sohbetNice Article .....Thanks for sharing

# April 28 2009, 00:05

sbs tr said:

sbsThank you for this informative read, I really appreciate sharing this great post. Keep up your work.

# May 07 2009, 23:13

David us said:

DavidI still use silverlight older version for applications. Have nt yet configured out the latest one.

# May 25 2009, 19:55

Chicago Movers br said:

Chicago Moversnice post
love u all

# June 07 2009, 09:29