我们如何在 C++/WinRt 项目中添加事件?

How do we add an event in C++/WinRt project?

我目前正在研究 C++/WinRt,它使用 WifiDirect 将(发送方)连接到特定区域周围的一个可用设备(客户端)。当设备想要连接时,它会向发送方发送连接请求。发送方需要检测客户端发送的连接请求,并连接到客户端。为此,我需要添加一个事件-(请求连接时)。我一添加它就应该执行 OnConnectionRequested 的代码。

#include "pch.h"
#include <winrt/Windows.Foundation.Collections.h>
#include "winrt/Windows.Devices.WiFiDirect.h"
#pragma once
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::WiFiDirect;
using namespace Windows::Storage::Streams;
using namespace winrt::Windows::Devices::WiFiDirect;
using namespace winrt::Windows::Devices::Enumeration;
enum class NotifyType
{
    StatusMessage,
    ErrorMessage
};
enum class CallbackContext
{
    Any,
    Same
};
class st
{
public:
    void OnConnectionRequested(WiFiDirectConnectionListener sender,
                               WiFiDirectConnectionRequestedEventArgsconnection EventArgs)
    {
        WiFiDirectConnectionRequest connectionRequest = connectionEventArgs.GetConnectionRequest();
        printf("Connection request received from ", connectionRequest.DeviceInformation().Name(), "Connection Request");
        printf("Connecting to ", connectionRequest.DeviceInformation().Name(), NotifyType::StatusMessage);
    }
    
    void start()
    {
        Windows::Devices::WiFiDirect::WiFiDirectAdvertisementPublisher _publisher;
        Windows::Devices::WiFiDirect::WiFiDirectConnectionListener _listener;
        winrt::event_token _connectionRequestedToken;
    
        try
        {
            _connectionRequestedToken = _listener.ConnectionRequested({this, &st::OnConnectionRequested});
            _publisher.Start();
            printf("Advertisement started, waiting for StatusChangedcallback...", NotifyType::StatusMessage);
        }
        catch (...)
        {
            printf("Error starting Advertisement: ", NotifyType::ErrorMessage);
        }

        getchar();
    }
};
int main()
{
    st s;
    s.start();
}

这是在 C++/WinRt 中添加事件的正确方法吗:

_connectionRequestedToken = _listener.ConnectionRequested({this, &st::OnConnectionRequested});

错误是:

LNK1120 1 unresolved externals - error in Winrt.exe file

LNK2019 unresolved external symbol "public: struct winrt::hstring __thiscall winrt::impl::consume_Windows_Devices_Enumeration_IDeviceInformation::Name(void)const " (?Name@?$consume_Windows_Devices_Enumeration_IDeviceInformation@UIDeviceInformation@Enumeration@Devices@Windows@winrt@@@impl@winrt@@QBE?AUhstring@3@XZ) referenced in function "public: void __thiscall st::OnConnectionRequested(struct winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionListener,struct winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionRequestedEventArgs)" (?OnConnectionRequested@st@@QAEXUWiFiDirectConnectionListener@WiFiDirect@Devices@Windows@winrt@@UWiFiDirectConnectionRequestedEventArgs@3456@@Z) - error in Program.obj 1

我应该在这一行做些什么修改来清除错误?该错误的实际含义是什么?或者有没有其他方法可以在 C++/WinRt 项目中添加事件?

您缺少 #include 指令:

#include <winrt/Windows.Devices.Enumeration.h>

Why is the linker giving me a "LNK2019: Unresolved external symbol" error?:

If the unresolved symbol is an API from the Windows namespace headers for the C++/WinRT projection (in the winrt namespace), then the API is forward-declared in a header that you've included, but its definition is in a header that you haven't yet included. Include the header named for the API's namespace, and rebuild. For more info, see C++/WinRT projection headers.

有关如何处理事件的一般说明,请参阅 Handle events by using delegates in C++/WinRT


this(原始指针)捕获到委托中也可能很危险。这打破了 link between object lifetime and visibility, and puts the burden of lifetime management on you. See Safely accessing the this pointer with an event-handling delegate 更安全、更易于管理的替代方案。