Beheeddine-8582 avatar image
0 Votes"
Beheeddine-8582 asked NicoZhu-MSFT edited

how do event revokers work

include <winrt/Windows.UI.ViewManagement.h>

include <winrt/Windows.Foundation.h>

include <iostream>

winrt::Windows::UI::Color accentColer;
winrt::Windows::UI::ViewManagement::UIColorType accentcollink = winrt::Windows::UI::ViewManagement::UIColorType::Accent;
struct CNotify
void NotifyColors()
colorValuesChangedRevoker = uiSettings.ColorValuesChanged(
winrt::auto_revoke, [this](winrt::Windows::UI::ViewManagement::UISettings const&, winrt::Windows::Foundation::IInspectable const&){this->UpdateAccentColor();}
std::cout << "slm" << std::endl;
winrt::Windows::UI::ViewManagement::UISettings uiSettings;
winrt::Windows::UI::ViewManagement::UISettings::ColorValuesChanged_revoker colorValuesChangedRevoker;
void UpdateAccentColor();

void CNotify::UpdateAccentColor()
accentColer = uiSettings.GetColorValue(accentcollink);
std::cout << "Accent Color :" << " A:" << int(accentColer.A) << " R:" << int(accentColer.R) << " G:" << int(accentColer.G) << " B:" << int(accentColer.B) << std::endl;

int main()
CNotify notify;
bool running = true;
while (running)
/ code /

this is working perfectly fine in vscode
so I have some questions

// Register
event_token ColorValuesChanged(TypedEventHandler<UISettings, IInspectable const&> const& handler) const;

// Revoke with event_token
void ColorValuesChanged(event_token const* cookie) const;

// Revoke with event_revoker
UISettings::ColorValuesChanged_revoker ColorValuesChanged(auto_revoke_t, TypedEventHandler<UISettings, IInspectable const&> const& handler) const;

this is the text in the developer options when I search for revokers in the website for this function it seems like it's for c# and as I understand you can use either the event_revoker or register the event and revoke it somehow my question is how to use the other method
+ what is the difference
+ {this->UpdateAccentColor();} this is what make the other function or assign the function to the event right then this is replacing this "const& handler"
right then my biggest question since everything is in c# is how to convert these examples somehow to c++
thanks for your help y'all

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT edited

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&)
         // ...


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 */)
         // ...


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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.