Hello,
Welcome to Microsoft Q&A!
C++/WinRT provided two ways cancel register event, the first is event_token
, it will return a winrt::event_token
instance after register a event handler. When you pass the token instance to the event handler, it could cancel this event.
For example
winrt::event_token token = m_button.Click([this](IInspectable const&, RoutedEventArgs const&)
{
// ...
});
m_button.Click(token);
The second way is using auto_revoke
, it will return revoker instance when register event. When this revoker is destructed, the event handler is automatically unregistered. it need to require WinRT types to support weak references when using auto_revoke. If weak references are not supported (such as types in the Windows.UI.Composition
namespace), a winrt::hresult_no_interface exception will occur.
m_event_revoker = button.Click(winrt::auto_revoke, [this](IInspectable const& /* sender */, RoutedEventArgs const& /* args */)
{
// ...
});
m_event_revoker.revoke()
And you could also call revoke method manually to cacel this event directly.
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.