I have an class in a Windows Runtime Component (written in C#) that raises events.
I cannot work out how to subscribe to these events in a C++/CX app that references the component.
The C# code (in the Windows Runtime Component):
public sealed class Messenger {
private EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>> messageReceivedTokenTable;
public event EventHandler<MessageReceivedEventArgs> MessageReceived
{
add
{
return EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>>
.GetOrCreateEventRegistrationTokenTable(ref this.messageReceivedTokenTable)
.AddEventHandler(value);
}
remove
{
EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>>
.GetOrCreateEventRegistrationTokenTable(ref this.messageReceivedTokenTable)
.RemoveEventHandler(value);
}
}
internal void OnMessageReceived(string message, string location)
{
EventHandler<MessageReceivedEventArgs> temp =
EventRegistrationTokenTable<EventHandler<MessageReceivedEventArgs>>
.GetOrCreateEventRegistrationTokenTable(ref this.messageReceivedTokenTable)
.InvocationList;
temp(this, new MessageReceivedEventArgs(message, location));
}
}
MessageReceivedEventArgs
is:
public sealed class MessageReceivedEventArgs : object
{
public MessageReceivedEventArgs(string message, string location)
{
this.Message = message;
this.SenderLocation = location;
}
public string Message { get; set; }
public string SenderLocation { get; set; }
}
Note that as per MSDN this descends from object and not EventArgs.
Then in C++:
msngr = ref new Messenger();
msngr->MessageReceived += ?????????
What should go after the +=
and in the relevant method (and anywhere else - in C# and/or C++) so that I can receive the messages in the C++ app?
I've tried various things and the various compiler warnings I've encountered have been unable to point me to a solution.
All examples I've found of using a Windows Runtime Component written in C# but consumed in a C++ app have been trivial and only show using properties and calling methods. Both of which I can do without problem. I'm after an example of subscribing to an event in C++ that is raised in C#.