How to subscribe to events created in the Windows Runtime component in C ++ / CX?

I have a class in a Windows Runtime component (written in C #) that fires events.
I cannot figure out how to subscribe to these events in a C ++ / CX application that references the component.

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:

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 according to MSDN, this comes from an object, not from EventArgs.

Then in C ++:

msngr = ref new Messenger();

msngr->MessageReceived += ?????????

What should be done after +=and in the appropriate method (and anywhere - in C # and / or C ++) so that I can receive messages in a C ++ application?

I tried different things, and the various compiler warnings I came across were not able to point me to a solution.

, Runtime Windows, #, ++, . . ++, #.

+2
3

- .

-/ COM MessageReceivedEventArgs # ++/CX. , . . WinRT, , .

ProxyStubForWinRTComponents_server, #, . Post-Build, :

call "$(DevEnvDir)..\..\VC\vcvarsall.bat" x86
winmdidl /outdir:"$(ProjectDir)\" "$(TargetPath)"

SDK-. winmdidl.exe, , .winmd, , IDL, . :

  • Microsoft.SDKSamples.Kitchen.idl - .winmd IDL .
  • Microsoft.SDKSamples.Kitchen.h - # ++, # ++/CX
  • Microsoft.SDKSamples.Kitchen_i.c - GUID #, -
  • Microsoft.SDKSamples.Kitchen_p.c - goo,
  • dlldata.c - .

ProxyStubsForWinRTComponentsPS, , DLL /. , winmdidl.exe, .def, DLL. COM , . , , .

, . , .

+4

, ref ++, , CPPClass, :

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);
}

=================

, , :

#:

namespace WindowsRuntimeComponent3
{
    public sealed class MyEventsArgs
    {
    }

    public sealed class Class1
    {
        public Class1()
        {
        }

        public event EventHandler<MyEventsArgs> MyEvent;
    }
}

++:

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);
}

=================

0

The official documentation for this scenario can be found here: Event Raising in Windows Runtime Components

0
source

All Articles