code_junkie

WPF animation slow when the mouse is in the window

Nov 13th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. let timer = new Threading.DispatcherTimer();
  2. timer.Tick.Add(fun _ -> Canvas.SetLeft(rect, Canvas.GetLeft(rect)+0.01) |> ignore)
  3. timer.Start()
  4.  
  5. timer.Interval <- TimeSpan.FromMilliSeconds(30.0)
  6.  
  7. #light
  8.  
  9. // When running inside fsi, this will install a WPF event loop
  10. #if INTERACTIVE
  11.  
  12. #I "c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.0";;
  13. #I "C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/";;
  14. #r "presentationcore.dll";;
  15. #r "presentationframework.dll";;
  16. #r "WindowsBase.dll";;
  17.  
  18. module WPFEventLoop =
  19. open System
  20. open System.Windows
  21. open System.Windows.Threading
  22. open Microsoft.FSharp.Compiler.Interactive
  23. open Microsoft.FSharp.Compiler.Interactive.Settings
  24. type RunDelegate<'b> = delegate of unit -> 'b
  25.  
  26. let Create() =
  27. let app =
  28. try
  29. // Ensure the current application exists. This may fail, if it already does.
  30. let app = new Application() in
  31. // Create a dummy window to act as the main window for the application.
  32. // Because we're in FSI we never want to clean this up.
  33. new Window() |> ignore;
  34. app
  35. with :? InvalidOperationException -> Application.Current
  36. let disp = app.Dispatcher
  37. let restart = ref false
  38. { new IEventLoop with
  39. member x.Run() =
  40. app.Run() |> ignore
  41. !restart
  42. member x.Invoke(f) =
  43. try disp.Invoke(DispatcherPriority.Send,new RunDelegate<_>(fun () -> box(f ()))) |> unbox
  44. with e -> eprintf "nn ERROR: %On" e; rethrow()
  45. member x.ScheduleRestart() = ()
  46. //restart := true;
  47. //app.Shutdown()
  48. }
  49. let Install() = fsi.EventLoop <- Create()
  50.  
  51. WPFEventLoop.Install();;
  52.  
  53. #endif
  54.  
  55. #light
  56.  
  57. open System
  58. open System.Windows
  59. open System.Windows.Controls
  60. open System.Windows.Shapes
  61. open System.Windows.Media
  62. open System.Windows.Media.Animation
  63.  
  64.  
  65. let rect = new Rectangle(Fill = new SolidColorBrush(Colors.Red), Height = 20., Width = 20., Name = "myRectangle")
  66.  
  67. let canvas =
  68. let c = new Canvas()
  69. c.Children.Add(rect) |> ignore
  70. c
  71.  
  72. NameScope.SetNameScope(canvas, new NameScope());
  73. canvas.RegisterName(rect.Name, rect)
  74.  
  75. let window = new Window(Content = canvas)
  76.  
  77. let nfloat f = new Nullable<float>(f)
  78.  
  79. let myDoubleAnimation = new DoubleAnimation(From = nfloat 20.0, To = nfloat 50.0,
  80. Duration = new Duration(TimeSpan.FromSeconds(5.)),
  81. RepeatBehavior = RepeatBehavior.Forever)
  82. let myStoryboard =
  83. let sb = new Storyboard()
  84. sb.Children.Add(myDoubleAnimation)
  85. sb
  86.  
  87. Storyboard.SetTargetName(myDoubleAnimation, rect.Name);
  88. Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty))
  89.  
  90. rect.Loaded.Add(fun _ -> myStoryboard.Begin canvas)
  91.  
  92. let main() =
  93. let app = new Application()
  94. app.Run window |> ignore
  95.  
  96. [<STAThread>]
  97. do main()
Add Comment
Please, Sign In to add comment