Resumé

Silverlight and F# happily ever after

Cristian Merighi () 5.00

New Visual Studio templates are available: embedding F# libraries into a Silverlight application is now super-easy!
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

This article is about F# and Silverlight, also known as "the perfect marriage" (thesis).

Silverlight, to me, is mostly about UI customization and animation effects (in other words I'm not attracted by standard reusable controls à la WebForms).
UI customization and animation effects, on their side, need strong mathematical programming.
This programming approach perfectly fits with F# language.
QED ;)

Jokes apart, Visual Studio templates for F# are finally available (here) and they target both version 2 and 3!
Now you can write libraries in F# that you can easily reference from your Silverlight application.

Let's build a sample one.
Goal:
Build a custom IValueConverter that translates a PointCollection into the area of the relevant generic Polygon.

Let's sketch the implementation step-by-step:

  • create a new F# Silverlight Class-Library project (that targets version 3);
  • implement a module that contains the necessary mathematical algorithms (in properly written F#);
  • reference the built-up library into your Silverlight C# application;
  • exploit the exposed code to implement the custom converter;
  • bake the whole thing with a trivial demo sample (see figure below).

live demo

See the template icons and project names available in Visual Studio:

F# projects

Now, the F# code.
Algorithm is pretty simple: given a sorted (counter-clockwise in a LH coordinates system) list of points in the 2D space (x, y), the area of any given polygon can be computed using the following formula
(Note: Silverlight uses a RH coordinates system)

formula

By translating it into F# code we obtain something like

#light

namespace Pacem.Science.Geometry

open System.Windows.Media

 

module public EuclideanGeometry =

    let public Area2D(polygon: PointCollection) =

        let n = polygon.Count

        let divideBy2 f = f * 0.5

        let step i = polygon.[i].X * polygon.[(i+1)%n].Y - polygon.[i].Y * polygon.[(i+1)%n].X

        {0..n-1}

        |> Seq.fold(fun acc i -> acc + step i ) 0.0

        |> divideBy2

Let's compile it and add as a reference in our Silverlight app (C#), then implement the custom IValueConverter:

public class AreaConverter : IValueConverter

{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

        if (value is System.Windows.Media.PointCollection)

        {

            double area = Pacem.Science.Geometry.EuclideanGeometry.Area2D(

                (System.Windows.Media.PointCollection)value

                );

            if (targetType == typeof(double))

                return area;

            if (targetType == typeof(string))

            {

                if (parameter is string)

                    return area.ToString((string)parameter, culture);

                else

                    return area.ToString(culture);

            }

        }

        throw new NotImplementedException();

    }

 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

        throw new NotImplementedException();

    }

}

The job is done. For the markup implementation, feel free to download and lurk the code from the link below.

Nota bene: demo and code have been implemented on top of Silverlight 3 Beta1, I do not tend nor intend to modify the content of this article to match the future releases of the Silverlight plugin.

zip file « Silverlight AreaConverter

Take care. Bye.

Feedbacks

  • Visual Studio 2010

    Tuomas Hietanen Tuesday, August 04, 2009 5.00

    Nice... I wish it would also work with Visual Studio 2010. But it doesn't. :-(

  • Re: Silverlight and F# happily ever after

    Art Scott Thursday, August 27, 2009 0.00

    Nice Cristian. Tuomas: OK I'm interested VS2010 ... any news on this working there?

  • VS2010 and Silverlight 4

    Cristian Merighi Saturday, November 13, 2010 0.00

    Cameron Taggart communicated to me that he has upgraded the sample project to target VS2010 and Silveright 4. http://blog.ctaggart.com/2010/11/ryoushins-2d-area-f-silverlight-demo.html

  • Re: Silverlight and F# happily ever after

    Cameron Taggart Monday, November 15, 2010 5.00

    And now I've translated it to all F# and it can be used from the free VS 2010 shell http://blog.ctaggart.com/2010/11/create-f-silverlight-apps-from-visual.html

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds