简体   繁体   中英

Event.create in f#?

I'm trying to compile the source code from : Custom WPF Controls in F#

How ever this line :

 let (handler, event) = Event.create<EventArgs>()

raises an error :

The value, constructor, namespace or type 'create' is not defined

The MSDN's Control.Event Module (F#) page does speak about such a function :

The additional functionality provided by the Event module is illustrated here. The following code example illustrates the basic use of Event.create to create an event and a trigger method, add two event handlers in the form of lambda expressions, and then trigger the event to execute both lambda expressions.

 type MyType() = let myEvent = new Event<_>() member this.AddHandlers() = Event.add (fun string1 -> printfn "%s" string1) myEvent.Publish Event.add (fun string1 -> printfn "Given a value: %s" string1) myEvent.Publish member this.Trigger(message) = myEvent.Trigger(message) let myMyType = MyType() myMyType.AddHandlers() myMyType.Trigger("Event occurred.") 

However note that it's only mentionned in the description, not in the example.

Also, the Control.Event Module (F#) page has no reference to such a create function.

I guess it might be an old function or something, but I'm new to F# so I can't see what it should be replaced with..

Event.create is a fairly old API for events, from before F# 2.0 judging by what's on MSDN. It gave you a trigger function and a published event - both of which now live as Publish and Trigger members of Event class.

So if you wanted to implement create in the 'modern' terms, it might look somewhat like this:

module Event = 
    let create<'T> () = 
        let event = Event<'T>()
        event.Trigger, event.Publish

I don't suggest you use it universally, but perhaps that's good enough to bring that old code back to life (the correct approach here being refactoring it to use Publish and Trigger instead of create ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM