A Simple Application in Silverlight Using F#

Introduction

As I have discussed in my previous article about windows and WPF applications using F#. Now I am taking one step Forward towards Silverlight application using F#. If you have Visual Studio 2010 you can make F# silverlight application with in one step.

If you are using an older version of Visual Studio like 2005 or 2008 than you also need to add one Xaml file, Html file and also a zip file with xap extension which will contain SilverlightFSharp.dll, System.Windows.Control.dll, FSharp.Core.dll and AppManifest.xaml to make Silverlight application in F#.

Here I am using Visual Studio 2010 and taking a simple example of Button clicking Event. When you make a project using F# silverlight application Template in Visual Studio 2010. You will get two files in solution Explorer one is MainPage.fs and Second one is App.fs. You will open MainPage.fs and write your code in this file.

The below code block will use to add Button control in application.

// Now here we are adding Button...
        let firstBtn = new Button(HorizontalAlignment = HorizontalAlignment.Center,
                             VerticalAlignment = VerticalAlignment.Center)
        firstBtn.Content <- "Please Click"                             
        firstBtn.Click.Add(fun _ ->
                      firstBtn.Content <- "I am a Button in SilverLight using f#")
        this.Content <- firstBtn

Note The complete code you can see in step 3.

Getting Started With Silverlight Application using F#

If you do not have F# Silverlight application Template in your Visual Studio 2010 you Can download it from below given link.

http://www.c-sharpcorner.com/Resources/1215/fsharp-silverlight-application-template.aspx

Step 1: Firstly Open a new Project in F# using Visual Studio 2010. Select F# Silverlight Application template and give name to the Project like below image.

New Project Dialog Box

Step 2: Now your solution explorer will contain two files MainPage.fs and App.fs as you can see in below image.

Solution Explorer

Step 3: Then Click on MainPage.fs file and write below code, your MainPage.fs window will look like below image.

Silverlight Example

namespace FSharpSilverlightApp
 
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Media.Imaging
 
type FirstApp = class
    inherit UserControl
    
    new () as this = {} then
        // Now here we are adding Button...
        let firstBtn = new Button(HorizontalAlignment = HorizontalAlignment.Center,
                             VerticalAlignment = VerticalAlignment.Center)
        firstBtn.Content <- "Please Click"                             
        firstBtn.Click.Add(fun _ ->
                      firstBtn.Content <- "I am a Button in SilverLight using f#")
        this.Content <- firstBtn
        
end
 
type SilverApp = class
    inherit Application
    
    new () as this = {} then
        this.Startup.Add(fun _ ->  this.RootVisual <- new FirstApp())
        //base.Exit.Add( fun _ -> ()) //this.Application_Exit)
        //this.InitializeComponent()
 
end 

Step 4: Now press F5 to Execute the code and your first Silverlight application  is ready.

Output

Silverlight Output1

Silverlight Output2

Silverlight Output3

Summary

In this article I have discussed about how you can make a Silverlight application using F#.

Up Next
    Ebook Download
    View all
    Learn
    View all