I’m too tired to write a proper blog post about this but I managed to port DonSyme’s Game of Life Demo that comes with the F# Distribution to Silverlight. It’s not a full port as there were some UI events that I couldn’t get to work for love nor money – I think these maybe bugs in Silverlight itself. However the game does look pretty cool. Also Silverlight currently has no way to marshal back to the UI thread which makes multithreading a bit difficult.

The game was relatively simple to port, in fact I kept the first few files, “alg.fsi”, “alg.fs” and “worker.fs” in tacked and just replaced the client.fs with the following:

#light

 

namespace SilverLife

 

open System;

open System.Collections.Generic;

open System.Windows.Browser;

open System.Threading;

open System.Windows

open System.Windows.Controls

open System.Windows.Documents

open System.Windows.Ink

open System.Windows.Input

open System.Windows.Media

open System.Windows.Media.Animation

open System.Windows.Shapes

 

type Page() = class

    inherit Canvas() as base

    member x.Page_Loaded(o : obj,  e : EventArgs) =

        let canvas = o :?> Canvas

        let gridSize = 23

       

        /// Create and configure the worker automata, ready to be placed

        /// onto a thread.

        let rec worker = Worker.mkWorker gridSize

       

        and grid =

            [| for x in 0 .. gridSize ->

                [| for y in 0 .. gridSize ->

                    let image = new Image()

                    image.Source <- new Uri("RedBloodcellOff.jpg", UriKind.Relative)

                    image.SetValue(Canvas.TopProperty, y * 14)

                    image.SetValue(Canvas.LeftProperty, x * 14)

                    canvas.Children.Add(image)

                    image |] |]

       

        and bornQueue = new Queue<Game.points>() 

        and diedQueue = new Queue<Game.points>() 

 

        // This is the refresh operation that will be invoked by the worker.

        and workerNotifyUpdates (born,died) =

            lock bornQueue (fun () -> bornQueue.Enqueue(born))

            lock diedQueue (fun () -> diedQueue.Enqueue(died))

            

            

 

        and workerThread = new Thread(start=(fun () -> worker.Start()), IsBackground = true, Priority = ThreadPriority.Lowest)

         

        worker.Updates.Add(workerNotifyUpdates)

        let dequeueAll (queue : Queue<Game.points>) image =

            while (queue.Count > 0) do

                let points = queue.Dequeue()

                List.iter (fun (x,y) -> grid.[x].[y].Source <- new Uri(image, UriKind.Relative) ) points

               

        let timer_Tick _ =

            lock bornQueue (fun () -> dequeueAll bornQueue "RedBloodcellOn.jpg")

            lock diedQueue (fun () -> dequeueAll diedQueue "RedBloodcellOff.jpg")

           

        let timer = new HtmlTimer()

        timer.Interval <- 1

        timer.Tick.Add(timer_Tick)

        timer.Enabled <- true

 

        workerThread.Start()

        ()

    member x.ClickSquare(sender : obj,  e : MouseEventArgs) = ()

end

You can see the game at the following URL (careful I’ve noticed it crashes the browser sometimes, I guess that’s what they mean by “alpha”):

http://www.strangelights.com/fsharp/silverlight/life.aspx

Of course you need the silverlight 1.1 alpha installed here’s a screen shot for those that don’t have that.

Feedback:

Feedback was imported from my only blog engine, it's no longer possible to post feedback here.

Silverlight Cream for May 25 - http://geekswithblogs.net/wynapsetechnicalmusings/archive/2007/05/25/112762.aspx

Silverlight Cream for May 25

Game of Life in Silverlight and F# - http://blogs.msdn.com/dsyme/archive/2007/05/25/game-of-life-in-silverlight-and-f.aspx

Game of Life in Silverlight and F#

Game of Life in Silverlight and F# - http://blogs.msdn.com/walterst/archive/2007/05/27/game-of-life-in-silverlight-and-f.aspx

Game of Life in Silverlight and F#

re: Game of Life in Silverlight and F# - Richard Hein

Great stuff, I wanted to put F# and Silverlight together as well. I am looking into how to best combine this with Monorail. Cheers, Richard Hein

re: Game of Life in Silverlight and F# - Robert Pickering

Hi Richard,

Thanks for the feedback. You maybe insterested in this more detailed post I've just written:
http://www.strangelights.com/blog/archive/2007/05/28/1585.aspx

I've not actually had chance to play with the monorail stuff but I'm looking forward to see what you come up with.

Cheers,
Rob

Silverlight Examples - Michael's Blog

During the weekend I spent some minutes to collect some of the greatest Silverlight examples. Most of

Non-Stop Mix 07 Paris - Richard J. Rothery Jr.

Robert,

Nice one F# and Silverlight!
Your are an Englishman a Paris...
I am and Amercican a Marseille...

See you at Mix 07 in Paris if you are planning to attend.

Best Regards,
Richard

【转】分享数十个silverlight示例和blog - Bryan Chow

分享几个国外的silverlight例子和blogGreatVisualStudioQuickStarts

re: Game of Life in Silverlight and F# - Sridhar Ratnakumar

I tried my best to compile, but even though I have Silverlight latest version installed I get this error -


C:\Users\srid>fsc -I "C:\Program Files\Microsoft Silverlight\2.0.30523.6" -r System.SilverLight.dll -r agclr.dll -r Microsoft.Scripting.Vestigial.dll -r System.
dll -r Microsoft.Scripting.Silverlight.dll -r Microsoft.Scripting.dll --clr-root "C:\Program Files\Microsoft Silverlight" --no-framework --no-mllib --static-lin
k fslib test.fs
Implicitly referencing 'C:\Program Files\Microsoft Silverlight\2.0.30523.6\System.dll'...

startup(1,0): error FS0191: Unable to find the file System.SilverLight.dll in any of
C:\Program Files\Microsoft Silverlight
C:\Program Files\Microsoft Silverlight\2.0.30523.6
C:\Users\srid
C:\Program Files\FSharp-1.9.4.17\bin.

re: Game of Life in Silverlight and F# - Ray

Pretty interesting. Though, there is little to no use of F# in this example as code pretty much imperative - about just same as if it was wrote in C#...

re: Game of Life in Silverlight and F# - Robert Pickering

Hi Ray,

I agree the layer that interacts with silverlight is pretty imperative, mainly because its a UI layer, which is the sample code presented in this blog. However layers that actually drive the simulation, which come from the F# example pack are good functional style. These were distributed with F# but now need to be downloaded from:
http://code.msdn.microsoft.com/fsharpsamples

Cheers,
Rob