in F#, I have the event defined as:
type ProgressNoteEvent() =
let event1 = new Event<string>()
let standardDotNetEventArgsEvent = new Event<EventHandler, EventArgs>()
[<CLIEvent>]
member this.Event1 = event1.Publish
[<CLIEvent>]
member this.StandardDotNetEventArgsEvent = standardDotNetEventArgsEvent.Publish
member this.TestEvent(arg) =
event1.Trigger(arg)
member this.TestStandardDotNetEventArgsEvent() =
standardDotNetEventArgsEvent.Trigger(this, EventArgs.Empty)
In the C# usercontrol code-behind, I have:
public ProgressNoteEvent progressNoteEvent;
progressNoteEvent = new ProgressNoteEvent();
progressNoteEvent.Event1 += ProgressNoteEvent_Event1;
progressNoteEvent.StandardDotNetEventArgsEvent += ProgressNoteEvent_StandardDotNetEventArgsEvent;
These events are raised by F# (in the DataContext for the WPF usercontrol):
let classWithEvent = new ProgressNoteEvent()
classWithEvent.Event1.Add(fun arg ->printfn "Event1 occurred! Object data: %s" arg)
classWithEvent.TestEvent("Hello World!")
classWithEvent.TestStandardDotNetEventArgsEvent()
I'm clearly missing something...The C# code in the code-behind never receives the raised event. What am I missing?
TIA
From your code sample, it seems you are creating two separate instances of the
ProgressNoteEventclass:progressNoteEvent = new ProgressNoteEvent())let classWithEvent = new ProgressNoteEvent())As those are two separate instances, triggering events in one would not cause events in the other. For this to work, the C# code needs to access the same instance as the F# code. Without knowing more about the rest of your project, it is hard to say how to best do this - but you need to create just one instance and pass it to the other project (probably create one in C# and pass it to the F# code). You could also make this global or the event static, but that is probably not going to make your code very elegant.