'How to define a member function as a delegate in C++/WinRT
I am trying to define a member function as a delegate for a C++/WinRT component. The component is something that I am trying to build to connect to a BLE device
The idl for the component defines the event as such
event Windows.Foundation.EventHandler<UInt32> ConnectNotification;
In the consuming app I am able to register a delegate for event's if I define it as a lambda function in this fashion
foundBleNotification = m_bleManager.ConnectNotification([](const auto&, uint32_t foundFlags)
{
OutputDebugStringW((L"Lambda Called handler->" + hstring(std::to_wstring(foundFlags)) +L"\r\n").c_str());
});
The above works fine and it gets invoked as expected,
However I would like to try and understand how to use a member function for the delegate instead of a lambda function, based on the idl I assumed I could simply define a member function as so...
void BleFound(uint32_t foundFlags);
And then register the notification by....
foundBleNotification = m_bleManager.ConnectNotification(&MainPage::BleFound };
This results in an error that I just cannot figure out :-(
Error C2511 'void winrt::HelloWorldWinRT::implementation::MainPage::BleFound(const winrt::implements<D,winrt::HelloWorldWinRT::MainPage,winrt::composing,winrt::Windows::UI::Xaml::Controls::IPageOverrides,winrt::Windows::UI::Xaml::Controls::IControlOverrides,winrt::Windows::UI::Xaml::Controls::IControlOverrides6,winrt::Windows::UI::Xaml::IFrameworkElementOverrides,winrt::Windows::UI::Xaml::IFrameworkElementOverrides2,winrt::Windows::UI::Xaml::IUIElementOverrides,winrt::Windows::UI::Xaml::IUIElementOverrides7,winrt::Windows::UI::Xaml::IUIElementOverrides8,winrt::Windows::UI::Xaml::IUIElementOverrides9,winrt::Windows::UI::Xaml::Markup::IComponentConnector,winrt::Windows::UI::Xaml::Markup::IComponentConnector2>::IInspectable &,uint32_t)': overloaded member function not found in 'winrt::HelloWorldWinRT::implementation::MainPage' HelloWorldWinRT C:\Users\Jay\source\repos\HelloWorldWinRT\HelloWorldWinRT\MainPage.cpp 66
I tried reading up on this here
https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/weak-references
But that has me even more confused...
Can some experts please point out what I am doing incorrectly and maybe define a member function that would work per the lambda function?
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
