Art*_*ott 2 wpf user-interface msdn f#
canvas.MouseMove.Add(move canvas update)
MouseMove.Add( p1 p2 p3)
Run Code Online (Sandbox Code Playgroud)
通常我会看到这个用法和文档,两个参数 - (对象发送者,MouseEventArgs e) - 我在这个示例代码中采用移动和画布,取自F#.NET Journal的计算几何:快速外壳.
更新一些delagate?或将气泡信息路由到MouseMove.Add?
我只是没有得到它.欢迎任何帮助.谢谢.艺术
kvb的答案给出了所有的理论,所以我想我也可以添加一个代码示例.我不知道片段的来源,所以我会根据名字做一些猜测 - 希望即使它可能与原始样本不完全相同也会有用.
正如kvb所说,代码move canvas update实际上是一个函数调用,它返回一个函数,然后将其作为处理程序添加到MouseMove事件中.
这意味着move可以声明如下:
let move (canvas:Canvas) (update:unit -> unit) (me:MouseEventArgs) =
// This function gets called when the mouse moves
// - values me.X and me.Y give the current mouse location
// - we can access 'canvas' that was declared when registering handler
// - we can call 'update' to do some more work...
// Pseudo-example:
canvas.Children.[0].Width <- me.X
update()
Run Code Online (Sandbox Code Playgroud)
注册事件处理程序时,代码move canvas update指定move函数的前两个参数,以便处理程序可以访问canvas可能在注册处理程序的位置声明的值(不使用可变变量!)
let canvas = new Canvas() // Create canvas
let update () = ... // some function that performs update
// Register handler and give it canvas and update as first two arguments
canvas.MouseMove.Add(move canvas update)
Run Code Online (Sandbox Code Playgroud)
这也应该解释为什么事件处理程序不需要sender:object作为第一个参数 - 你可以canvas使用部分函数应用程序以静态类型的方式将(它是发送者)作为第一个参数传递(因此您不必进行强制转换)objectto Canvas).