I have two suggestions:
1) if you use f # interactive check, you have set up a WFP event loop. F # interactive sets the winforms eventloop event by default, there is some compatibility between winforms and WFP event loops, but not everything works correctly. To set the event loop, use the following script:
#light
#if INTERACTIVE
#I "c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.0";;
#I "C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/";;
#r "presentationcore.dll";;
#r "presentationframework.dll";;
#r "WindowsBase.dll";;
module WPFEventLoop =
open System
open System.Windows
open System.Windows.Threading
open Microsoft.FSharp.Compiler.Interactive
open Microsoft.FSharp.Compiler.Interactive.Settings
type RunDelegate<'b> = delegate of unit -> 'b
let Create() =
let app =
try
let app = new Application() in
new Window() |> ignore;
app
with :? InvalidOperationException -> Application.Current
let disp = app.Dispatcher
let restart = ref false
{ new IEventLoop with
member x.Run() =
app.Run() |> ignore
!restart
member x.Invoke(f) =
try disp.Invoke(DispatcherPriority.Send,new RunDelegate<_>(fun () -> box(f ()))) |> unbox
with e -> eprintf "\n\n ERROR: %O\n" e; rethrow()
member x.ScheduleRestart() = ()
}
let Install() = fsi.EventLoop <- Create()
WPFEventLoop.Install();;
#endif
2) , , , . , ( width , ):
#light
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Shapes
open System.Windows.Media
open System.Windows.Media.Animation
let rect = new Rectangle(Fill = new SolidColorBrush(Colors.Red), Height = 20., Width = 20., Name = "myRectangle")
let canvas =
let c = new Canvas()
c.Children.Add(rect) |> ignore
c
NameScope.SetNameScope(canvas, new NameScope());
canvas.RegisterName(rect.Name, rect)
let window = new Window(Content = canvas)
let nfloat f = new Nullable<float>(f)
let myDoubleAnimation = new DoubleAnimation(From = nfloat 20.0, To = nfloat 50.0,
Duration = new Duration(TimeSpan.FromSeconds(5.)),
RepeatBehavior = RepeatBehavior.Forever)
let myStoryboard =
let sb = new Storyboard()
sb.Children.Add(myDoubleAnimation)
sb
Storyboard.SetTargetName(myDoubleAnimation, rect.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty))
rect.Loaded.Add(fun _ -> myStoryboard.Begin canvas)
let main() =
let app = new Application()
app.Run window |> ignore
[<STAThread>]
do main()