问题
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#.
回答1:
it's necessary create a proxy to use such types.
That's indeed your problem, a COM proxy/stub is required to marshal your MessageReceivedEventArgs class from C# to C++/CX. And yes, very poorly documented. I'll take a stab at explaining the process. Get started with this WinRT sample, it demonstrates the way to setup a solution to get the proxy you need and does exactly what you want to do.
The starting point is the ProxyStubForWinRTComponents_server project, a C# project that declares the shared classes. The important part of the project is the Post-Build event, it looks like this:
call "$(DevEnvDir)..\..\VC\vcvarsall.bat" x86
winmdidl /outdir:"$(ProjectDir)\" "$(TargetPath)"
The first statement sets up the environment to run SDK tools. The second step runs winmdidl.exe, a completely undocumented build tool that decompiles the .winmd file generated by the project into an IDL file and then compiles that. Output of this build step are:
- Microsoft.SDKSamples.Kitchen.idl - the decompiled .winmd file in IDL format and used to generate the rest of the files
- Microsoft.SDKSamples.Kitchen.h - contains the declarations of the C# types in C++ format, suitable to be #included in your C++/CX project
- Microsoft.SDKSamples.Kitchen_i.c - contains the GUIDs of the C# types, used to build the proxy
- Microsoft.SDKSamples.Kitchen_p.c - contains the goo from which the proxy and stub are generated
- dlldata.c - used to build the proxy.
Next look at the ProxyStubsForWinRTComponentsPS project, that's the one that builds the proxy/stub DLL. It consumes the files generated by winmdidl.exe, the only added one is the .def file that declares the exports from the DLL. COM calls those to use the proxy, you can use the file as-is. It is best to use this project as-is, only changing the names of the files, so you've got all the compiler and linker settings correct.
Unpleasant and lots of ways to get this wrong, no doubt. Hope it helps.
回答2:
Assuming you are writing this code in a C++ ref class, let's say CPPClass, you code would look like:
CPPClass::MyEventHandler(Platform::Object^ obj, MessageReceivedEventArgs^ args)
{
// handler code
}
CPPClass::SomeMethod()
{
msngr = ref new Messenger(this->SpecifedServer->Text);
msngr->MessageReceived += ref new EventHandler<MessageReceivedEventArgs^>(this, &CPPClass::MyEventHandler);
}
=================
By the way, below is a similar sample code that works just fine for me:
Defining the class and event in C#:
namespace WindowsRuntimeComponent3
{
public sealed class MyEventsArgs
{
}
public sealed class Class1
{
public Class1()
{
}
public event EventHandler<MyEventsArgs> MyEvent;
}
}
Consuming the event in C++:
App::App()
{
InitializeComponent();
Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
auto obj = ref new WindowsRuntimeComponent3::Class1();
obj->MyEvent += ref new EventHandler<WindowsRuntimeComponent3::MyEventsArgs^>(this, &App::MyEventHandler);
}
=================
回答3:
Official documentation for this scenario can be found on MSDN here: Raising Events in Windows Runtime Components
来源:https://stackoverflow.com/questions/14667871/how-to-subscribe-to-events-raised-within-a-windows-runtime-component-in-c-cx