how do event revokers work

Behe eddine 41 Reputation points
2022-05-12T16:28:00.633+00:00

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;
}
private:
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()
{
winrt::init_apartment();
CNotify notify;
notify.NotifyColors();
bool running = true;
while (running)
{
/* code */
}
return(0);
}

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
Universal Windows Platform (UWP)
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2022-05-13T02:54:43.477+00:00

    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.

    0 comments No comments