Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 当鼠标在窗口中时,WPF动画会变慢_.net_Wpf_F# - Fatal编程技术网

.net 当鼠标在窗口中时,WPF动画会变慢

.net 当鼠标在窗口中时,WPF动画会变慢,.net,wpf,f#,.net,Wpf,F#,我正在尝试创建一个移动矩形(rect)。以下是相关代码: let timer = new Threading.DispatcherTimer(); timer.Tick.Add(fun _ -> Canvas.SetLeft(rect, Canvas.GetLeft(rect)+0.01) |> ignore) timer.Start() 问题是,如果我将鼠标移到窗口上,动画会变慢。如果我设置计时器的间隔: timer.Interval <- TimeSpan.FromMil

我正在尝试创建一个移动矩形(rect)。以下是相关代码:

let timer = new Threading.DispatcherTimer();
timer.Tick.Add(fun _ -> Canvas.SetLeft(rect, Canvas.GetLeft(rect)+0.01) |> ignore)
timer.Start()
问题是,如果我将鼠标移到窗口上,动画会变慢。如果我设置计时器的间隔:

timer.Interval <- TimeSpan.FromMilliSeconds(30.0)

timer.Interval我有两个建议:

1) 如果您使用的是f#interactive,请检查您是否安装了WFP事件循环。F#interactive默认情况下设置winforms事件循环,winforms和WFP事件循环之间存在一些兼容性,但并非所有功能都能正常工作。要安装事件循环,请使用以下脚本:

#light

// When running inside fsi, this will install a WPF event loop
#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 
                // Ensure the current application exists. This may fail, if it already does.
                let app = new Application() in 
                // Create a dummy window to act as the main window for the application.
                // Because we're in FSI we never want to clean this up.
                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() =   ()
                 //restart := true;
                 //app.Shutdown()
        } 
    let Install() = fsi.EventLoop <-  Create()

WPFEventLoop.Install();;

#endif
#灯
//在fsi内部运行时,将安装WPF事件循环
#如果互动
#I“c:/Program Files/Reference Assembly/Microsoft/Framework/v3.0”;;
#I“C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/”;;
#r“presentationcore.dll”;;
#r“presentationframework.dll”;;
#r“WindowsBase.dll”;;
模块WPFEventLoop=
开放系统
开放系统
打开System.Windows.Threading
打开Microsoft.FSharp.Compiler.Interactive
打开Microsoft.FSharp.Compiler.Interactive.Settings

键入RunDelegate我有两个建议:

1) 如果您使用的是f#interactive,请检查您是否安装了WFP事件循环。F#interactive默认情况下设置winforms事件循环,winforms和WFP事件循环之间存在一些兼容性,但并非所有功能都能正常工作。要安装事件循环,请使用以下脚本:

#light

// When running inside fsi, this will install a WPF event loop
#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 
                // Ensure the current application exists. This may fail, if it already does.
                let app = new Application() in 
                // Create a dummy window to act as the main window for the application.
                // Because we're in FSI we never want to clean this up.
                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() =   ()
                 //restart := true;
                 //app.Shutdown()
        } 
    let Install() = fsi.EventLoop <-  Create()

WPFEventLoop.Install();;

#endif
#灯
//在fsi内部运行时,将安装WPF事件循环
#如果互动
#I“c:/Program Files/Reference Assembly/Microsoft/Framework/v3.0”;;
#I“C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/”;;
#r“presentationcore.dll”;;
#r“presentationframework.dll”;;
#r“WindowsBase.dll”;;
模块WPFEventLoop=
开放系统
开放系统
打开System.Windows.Threading
打开Microsoft.FSharp.Compiler.Interactive
打开Microsoft.FSharp.Compiler.Interactive.Settings

输入rundelegates谢谢你的建议。因为你说F#interactive可能有问题,所以我用C#做了同样的例子。C#代码也不起作用。我的问题是,每滴答0.01的移动量太低了。我认为1.0将是整个窗口,因此0.01将是窗口的1%。如果我将0.01更改为1.0,则动画工作正常!显然,如果不为计时器设置时间间隔,它将以尽可能快的速度运行(因此,尽管0.01很低,它仍会移动)。WPF事件循环非常有用。到目前为止,我一直在重置F#interactive。谢谢你的故事板。不幸的是,我不能使用故事板解决方案,因为动画不是直线。我正在尝试创建一个有引力的行星模拟,以了解更多关于WPF的信息。谢谢你的帮助!谢谢你的建议。因为你说F#interactive可能有问题,所以我用C#做了同样的例子。C#代码也不起作用。我的问题是,每滴答0.01的移动量太低了。我认为1.0将是整个窗口,因此0.01将是窗口的1%。如果我将0.01更改为1.0,则动画工作正常!显然,如果不为计时器设置时间间隔,它将以尽可能快的速度运行(因此,尽管0.01很低,它仍会移动)。WPF事件循环非常有用。到目前为止,我一直在重置F#interactive。谢谢你的故事板。不幸的是,我不能使用故事板解决方案,因为动画不是直线。我正在尝试创建一个有引力的行星模拟,以了解更多关于WPF的信息。谢谢你的帮助!