Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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
有没有办法在F#中识别CLR事件实例?_F# - Fatal编程技术网

有没有办法在F#中识别CLR事件实例?

有没有办法在F#中识别CLR事件实例?,f#,F#,当我在F#Interactive中工作时,我经常想对事件处理程序进行更改。简单地对事件调用Subscribe、Add或AddHandler函数会导致继续调用旧事件,这很少是我们的意图 一种解决方案是使用它返回的IDisposable,但这需要在您自己的代码中跟踪IDisposable,这对于探索性任务来说很麻烦 我已尝试制作一个字典,在再次订阅同一事件时调用Dispose(): let events = Dictionary<obj, IDisposable>() let subon

当我在F#Interactive中工作时,我经常想对事件处理程序进行更改。简单地对事件调用Subscribe、Add或AddHandler函数会导致继续调用旧事件,这很少是我们的意图

一种解决方案是使用它返回的IDisposable,但这需要在您自己的代码中跟踪IDisposable,这对于探索性任务来说很麻烦

我已尝试制作一个
字典
,在再次订阅同一事件时调用Dispose():

let events = Dictionary<obj, IDisposable>()
let subonce (e:IEvent<'h,'e>) (handler: 'e -> unit) =
    if events.ContainsKey e then
        events.[e].Dispose()
        events.Remove e |> ignore
    let d = e.Subscribe handler
    events.Add (e,d) |> ignore

let w = Window()
w.Show()

//Running this line in FSI a second time onward should Dispose() the previous subscription
subonce w.MouseUp (fun e -> printfn "%A" <| e.GetPosition(w))

在一个IEvent中是否有任何属性或字段可以根据所有者的其他实例以及该所有者中的不同事件来识别它?

这并不是问题的答案,但我想不出有多少其他场景需要识别事件实例,所以这可能就足够了:

type OneHandler<'e> = { mutable h : 'e -> unit }
let onehandler (e:IEvent<'h,'e>) =
    let h = { h = fun _ -> () }
    e.Subscribe(fun e -> h.h e) |> ignore
    h

let w = Window()
let wmouseup = onehandler w.MouseUp

wmouseup.h <- (fun e -> printfn "%A" <| e.GetPosition(w))
type OneHandler unit}
let onehandler(e:IEvent)=
设h={h=fun}
e、 订阅(乐趣e->h.h e)|>忽略
H
设w=Window()
设wmouseup=onehandler w.MouseUp

wmouseup.h printfn“%A”这是一个非常好的问题。我做了一些实验,但我找不到一种方法通过一个简单的
subonce
函数来实现这一点-我想你需要为函数提供更多信息,比如一些字符串用作键…@TomasPetricek严格来说,这不是这个问题的答案,但我最终编写了一个函数
IEvent->Handler
,其中Handler就是
type Handler unit}
。然后在我定义了
w
之后,我可以调用
let wmouseup=onehandler w.MouseUp
,然后从那时起,为了更改处理程序,我执行
wmouseup.h…
onehandler
也很简单:
let onehandler(e:IEvent)=let h={h=fun}在e.Subscribe(fune->h.hee)|>忽略;h
这看起来像是我最终可能会做的事情!你能把这个作为你问题的答案吗?“我认为这是最好的选择。”托马斯佩特里切克说。
type OneHandler<'e> = { mutable h : 'e -> unit }
let onehandler (e:IEvent<'h,'e>) =
    let h = { h = fun _ -> () }
    e.Subscribe(fun e -> h.h e) |> ignore
    h

let w = Window()
let wmouseup = onehandler w.MouseUp

wmouseup.h <- (fun e -> printfn "%A" <| e.GetPosition(w))