F# WPF 鼠标移动参数

发布于 2024-12-28 16:21:40 字数 276 浏览 0 评论 0原文

canvas.MouseMove.Add(move canvas update)

MouseMove.Add( p1 p2 p3)

通常我会看到这个用法和文档,两个参数—— (对象发送者,MouseEventArgs e) -- 在本示例代码中,我将其视为 move 和 canvas,摘自 F#.NET Journal 的 计算几何:快速船体。

更新有延迟吗?或将气泡信息路由到 MouseMove.Add?

我只是不明白。欢迎任何帮助。谢谢。艺术

canvas.MouseMove.Add(move canvas update)

MouseMove.Add( p1 p2 p3)

Usually I see this use and documentation, two params --
(object sender, MouseEventArgs e)
-- which I take to be move and canvas in this example code, taken from F#.NET Journal's
Computational geometry: quick hull.

Is update some delagate? or routing bubble info to MouseMove.Add?

I'm just not gettng it. Any help welcome. Thanks. Art

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-01-04 16:21:40

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()

注册事件处理程序时,代码 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) 

这也应该解释为什么事件处理程序不需要将 sender:object 作为第一个参数 - 你可以使用静态类型的方式使用部分函数应用程序将画布(即发送者)作为第一个参数传递(因此您不必强制转换对象) /code> 到 Canvas)。

The answer from kvb gives all the theory, so I thought I could also add a code sample. I don't know the source of the snippet, so I'll make some guesses based on the names - hopefully it will be useful even if it may not be completely same as the original sample.

As kvb says, the code move canvas update is actually a function call that returns a function, which is then added as a handler to the MouseMove event.

This means that move could be declared as follows:

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()

When registering the event handler, the code move canvas update specifies the first two arguments of the move function, so that the handler can access canvas value that was probably declared in the place where the handler is registered (without the use of mutable variables!)

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) 

This should also explain why the event handler does not need to take sender:object as the first argument - you can pass the canvas (which is the sender) as a first argument using partial function application in a statically typed way (so you don't have to cast object to Canvas).

烂人 2025-01-04 16:21:40

除非您有拼写错误,否则 move canvas update 是将函数 move 应用于值 canvasupdate,您没有显示其中任何一个;这不是传递一组 3 个独立的参数。

一般来说,F# 事件的工作方式与其他 .NET 语言中的工作方式略有不同。使用 Add 方法时,您传递的函数接受单个 EventArgs 子类,而不是发送者和事件参数。如果您想从处理程序中访问发送者,请改用AddHandler;通常这是不必要的,因为发送者只是您要添加处理程序的对象,因此您不需要从处理程序内获取对它的单独引用。

Unless you've got a typo, move canvas update is the application of the function move to the values canvas and update, none of which you are showing; this is not a set of 3 separate arguments being passed.

In general, in F# events work slightly differently than they do in other .NET languages. When you use the Add method, you pass a function which takes a single EventArgs subclass, not the sender and the event args. If you want to access the sender from within the handler, then use AddHandler instead; usually this isn't necessary because the sender is just the object on which you're adding the handler, so you don't need to get a separate reference to it from within the handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文