winrt:: event 結構範本(C++/WinRT)winrt::event struct template (C++/WinRT)

一種類型,可讓您用來宣告和執行指定委派類型的事件。A type that you can use to declare and implement an event of a specified delegate type. 訂閱者會將其事件處理委派傳遞給事件;事件會在集合中註冊這些委派;然後,當引發時,事件會依序叫用其已註冊的委派,讓訂閱者可以處理事件。Subscribers pass their event-handling delegates to an event; the event registers those delegates in a collection; then, when it is raised, the event invokes its registered delegates in turn so that subscribers can handle the event. 如需撰寫事件和程式碼範例的詳細資訊,請參閱在C++/WinRT 中撰寫事件For more info about authoring events, and code examples, see Author events in C++/WinRT.

如果您需要專案的內部事件,而不限於 Windows 執行階段類型,請使用事件<winrt::d 委派>。If you need events internal to your project, and not limited to Windows Runtime types, then use event<winrt::delegate>. 如需詳細資訊,請參閱參數化委派、簡單信號和專案內的回呼For more info, see Parameterized delegates, simple signals, and callbacks within a project.

如需處理事件和程式碼範例的詳細資訊,請參閱在/WinRT 中C++使用委派來處理事件For more info about handling events, and code examples, see Handle events by using delegates in C++/WinRT.

語法Syntax

template <typename Delegate>
struct event

範本參數Template parameters

typename Delegate 可以註冊以處理事件的委派類型。typename Delegate The type of delegate that can register to handle the event.

需求Requirements

最低支援 SDK: Windows SDK 版本10.0.17134.0 (Windows 10,版本1803)Minimum supported SDK: Windows SDK version 10.0.17134.0 (Windows 10, version 1803)

命名空間: winrtNamespace: winrt

標頭% WindowsSdkDir% Include<WindowsTargetPlatformVersion > \cppwinrt\winrt\base.h (預設包含)Header %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h (included by default)

成員類型別名Member type aliases

別名名稱Alias name 類型Type
事件::d elegate_typeevent::delegate_type typename Delegate 範本參數的同義字。A synonym for the typename Delegate template parameter.

建構函式Constructors

建構函式Constructor 描述Description
event:: event 函數event::event constructor 初始化事件結構的新實例。Initializes a new instance of the event struct.

成員函式Member functions

函數Function 描述Description
event:: add 函數event::add function 事件物件註冊委派。Registers a delegate with the event object.
event:: remove 函式event::remove function 撤銷委派從事件物件的註冊。Revokes a delegate's registration from the event object.

成員運算子Member operators

運算子Operator 描述Description
event:: operator ()(函式呼叫運算子)event::operator() (function call operator) 使用提供的引數叫用所有事件物件的已註冊委派。Invokes all of the event object's registered delegates with the provided arguments.
event:: operator boolevent::operator bool 檢查事件物件是否有任何已向它註冊的委派。Checks whether the event object has any delegates registered with it.

event:: event 函數event::event constructor

初始化事件結構的新實例。Initializes a new instance of the event struct.

語法Syntax

event();

event:: add 函數event::add function

事件物件註冊委派。Registers a delegate with the event object.

語法Syntax

winrt::event_token add(Delegate const& delegate);

參數Parameters

delegate 要向事件物件註冊的委派。delegate A delegate to register with the event object.

傳回值Return value

Winrt:: event_token ,之後可以用來撤銷委派的註冊。A winrt::event_token that can subsequently be used to revoke the delegate's registration.

event:: operator ()(函式呼叫運算子)event::operator() (function call operator)

使用提供的引數叫用所有事件物件的已註冊委派。Invokes all of the event object's registered delegates with the provided arguments.

語法Syntax

template<typename... Arg>
void operator()(Arg const&... args)

範本參數Template parameters

typename... Arg variadic 範本參數套件,其中包含呼叫委派時所傳遞之參數的類型。typename... Arg A variadic template parameter pack containing the types of the parameters that the delegate is passed when it's called.

參數Parameters

args 變數引數清單,其中包含呼叫委派時所傳遞的引數。args A variable argument list containing the arguments that the delegate is passed when it's called.

event:: operator boolevent::operator bool

檢查事件物件是否有任何已向它註冊的委派。Checks whether the event object has any delegates registered with it.

語法Syntax

explicit operator bool() const noexcept;

傳回值Return value

true事件物件是否有任何已註冊的委派,則為,否則為 falsetrue if the event object has any registered delegates, otherwise false.

event:: remove 函式event::remove function

撤銷委派從事件物件的註冊。Revokes a delegate's registration from the event object.

語法Syntax

void remove(winrt::event_token const token);

參數Parameters

token winrt:: event_token ,識別要撤銷其註冊的委派。token A winrt::event_token that identifies the delegate whose registration to revoke.

另請參閱See also