Hello,

Running the Try F# console offline is not possible. You have to use it on the site at [link:tryfsharp.org].

I am afraid that "init.fsx" would not be very interesting. The script only references a few DLLs using #r.

Here is a possible way to switch between F# interactive in Visual Studio (or at the command prompt). It’s only partially thought out and won’t fit every situations but it may be interesting for some cases.

The idea is to write a script that defines mock objects. A partial implementation of the API from the Try F# objects might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#I @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"
#r @"WindowsBase.dll"
#r @"PresentationCore.dll"
#r @"PresentationFramework.dll"
#r @"System.Xaml.dll"
namespace Microsoft.TryFSharp
 open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media
 
type CanvasPosition = Alone | Right | Hidden
 
type Console() = 
 
    let canvas = new Canvas()   
    let w = new Window()
    do
        w.Title <- "Try F# Like Environment for F# Interactive in Visual Studio"
        w.Content 
<- canvas
    
    member v.Canvas with get() = canvas
    member v.Show() = 
        if (w.IsVisible=false) then w.Show()
        w.Activate() |> ignore
    member v.ClearCanvas() = canvas.Children.Clear()
    member v.CanvasPosition with get() = Alone and set value = ()
 
type App() =
    static let console = new Console()   
    static member Show() = console.Show()
    static member Console with get() = console
    static member Canvas with get() = console.Canvas
    static member Dispatch f = console.Show(); f()

Then one could #load “Offline.fsx” in F# interactive running offline and invoke scripts that also run online at
[link:tryfsharp.org]. For example, the first example from the "Plain and fancy clocks" section in the "Building Rich User Interfaces" chapter of Try F# is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media
open Microsoft.TryFSharp
 
let clock1() =
    let canvas = App.Console.Canvas
    let label = new TextBlock(FontSize = 20.0)
    canvas.Children.Add label |> ignore
    Canvas.SetTop(label, 20.0)
    Canvas.SetLeft(label, 20.0)
 
    let updateLabel() =
        label.Text <- string System.DateTime.Now 
    updateLabel()
 
    let b = new Button(Content="Show current time")
    b.Click.Add(fun args -> updateLabel())
    canvas.Children.Add b |> ignore
    Canvas.SetTop(b, 60.0)
    Canvas.SetLeft(b, 20.0)
 
App.Dispatch(fun () ->
    clock1()
    App.Console.CanvasPosition <- CanvasPosition.Right
)

This script would run as is in F# interactive offline. A problem with this approach is that when you are in Visual Studio the target framework is the .NET Framework. You would have to be careful to only write code that is also compatible with Silverlight, which is the target framework in the browser.

Another approach would be to use the mock objects directly in a Visual Studio project for a Silverlight 4 application. It would avoid the target framework issue but it would not provide the interactive prompt. Instead, you would run your Silverlight application to see the output from your code.

I hope these pointers will help. Let us know how it goes.

Christophe

By on 7/20/2011 9:22 AM ()

Hi Christophe,

Thank you very much for the reply. I'm doing it exactly as you suggested, and it is working very well.

I have to be pretty careful with the differences between WPF and Silverlight though - why would Silverlight not define Colors.DeepPink?

Anyway, if I run into particular problems I'll write back.

I'm yet to try F# Snippets, but it would be nice to be able to structure the input a bit - allow more than just a single file, so have some form of #input feature - maybe #input from F# snippets....?

(Though I also see the F# snippets allows me to hide the boilerplate bits.)

Thanks for the help!

-Govert

By on 7/27/2011 3:08 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper