Search code examples
windows-runtimec++-winrtcppwinrt

How should I use AddHandler in cppwinrt with Handler objects?


In the C++/WinRT reflection, the API AddHandler is defined as

UIElement::AddHandler(Windows::UI::Xaml::RoutedEvent const& routedEvent, Windows::foundation::IInspectable const& handler, bool handledEventsToo) const

But the handlers seems to only inherit from IUnknown. For example, this is the declaration of KeyEventHandler:

struct KeyEventHandler : Windows::foundation::IUnknown

What is the solution for registering an event handler?


Solution

  • It seems like we need to call box_value(handler) which will convert it into an IInspectable. It is not obvious at all, and you cannot find any samples on MSDN.