Silverlight and F# happily ever after
Cristian Merighi ()

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).

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

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)

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.
«
Silverlight AreaConverter
Take care. Bye.