Visual Basic 和 WPF 事件處理Visual Basic and WPF Event Handling

特別針對 Microsoft Visual Basic .NET 語言,您可以使用特定語言的 Handles 關鍵字,將事件處理常式與實例產生關聯,而不是附加具有屬性的事件處理常式或使用 AddHandler 方法。For the Microsoft Visual Basic .NET language specifically, you can use the language-specific Handles keyword to associate event handlers with instances, instead of attaching event handlers with attributes or using the AddHandler method. 不過,將處理常式附加至執行個體的 Handles 技術有一些限制,因為 Handles 語法無法支援 WPFWPF 事件系統中某些特定的路由事件功能。However, the Handles technique for attaching handlers to instances does have some limitations, because the Handles syntax cannot support some of the specific routed event features of the WPFWPF event system.

在 WPF 應用程式中使用「Handles」Using "Handles" in a WPF Application

使用 Handles 連線到執行個體和事件的事件處理常式全需定義於執行個體的部分類別宣告中,這也是透過元素上的屬性值所指派之事件處理常式的需求。The event handlers that are connected to instances and events with Handles must all be defined within the partial class declaration of the instance, which is also a requirement for event handlers that are assigned through attribute values on elements. 您只能針對具有 Name 屬性值(或已宣告x:Name指示詞)之頁面上的元素指定 HandlesYou can only specify Handles for an element on the page that has a Name property value (or x:Name Directive declared). 這是因為 XAMLXAML 中的 Name 會建立支援實例所需的實例參考。 Handles 語法所需的事件參考格式。This is because the Name in XAMLXAML creates the instance reference that is necessary to support the Instance.Event reference format required by the Handles syntax. 唯一可以用於沒有 Name 參考之 Handles 的元素,是定義部分類別的根項目實例。The only element that can be used for Handles without a Name reference is the root-element instance that defines the partial class.

您可以將相同的處理常式指派給多個元素,方法是在 Handles 之後使用逗號來隔開 Instance.Event 參考。You can assign the same handler to multiple elements by separating Instance.Event references after Handles with commas.

您可以使用 Handles,將多個處理常式指派給同一個 Instance.Event 參考。You can use Handles to assign more than one handler to the same Instance.Eventreference. 不要為 Handles 參考中指定處理常式的順序指派任何重要性;您應該假設可以任何順序叫用處理同一個事件的處理常式。Do not assign any importance to the order in which handlers are given in the Handles reference; you should assume that handlers that handle the same event can be invoked in any order.

若要移除在宣告中以 Handles 加入的處理常式,您可以呼叫 RemoveHandlerTo remove a handler that was added with Handles in the declaration, you can call RemoveHandler.

您可以使用 Handles 來附加路由事件的處理常式,只要您將處理常式附加至會在其成員資料表中定義要處理事件的執行個體。You can use Handles to attach handlers for routed events, so long as you attach handlers to instances that define the event being handled in their members tables. 針對路由事件,使用 Handles 所附加的處理常式會遵循與附加為 XAMLXAML 屬性的處理常式相同的路由規則,或使用 AddHandler的一般簽章。For routed events, handlers that are attached with Handles follow the same routing rules as do handlers that are attached as XAMLXAML attributes, or with the common signature of AddHandler. 這表示如果事件已標示為已處理(事件資料中的 Handled 屬性 True),則不會叫用與 Handles 附加的處理常式來回應該事件實例。This means that if the event is already marked handled (the Handled property in the event data is True), then handlers attached with Handles are not invoked in response to that event instance. 事件可能是透過路由中另一個元素上的執行個體處理常式標示為已處理,或是透過路由上目前元素或先前元素上所處理的類別來標示。The event could be marked handled by instance handlers on another element in the route, or by class handling either on the current element or earlier elements along the route. 對於支援配對的通道/事件反昇事件的輸入事件,通道路由可能已將事件配對標示已處理。For input events that support paired tunnel/bubble events, the tunneling route may have marked the event pair handled. 如需路由事件的詳細資訊,請參閱路由事件概觀For more information about routed events, see Routed Events Overview.

加入處理常式的「Handles」限制Limitations of "Handles" for Adding Handlers

Handles 不能參考附加事件的處理常式。Handles cannot reference handlers for attached events. 您必須針對該附加事件使用 add 存取子方法,或 XAMLXAML 中的 typename.eventname 事件屬性。You must use the add accessor method for that attached event, or typename.eventname event attributes in XAMLXAML. 如需詳細資訊,請參閱路由事件概觀For details, see Routed Events Overview.

針對路由事件,您只能使用 Handles 來指派執行個體的處理常式,該事件存在於執行個體成員資料表中。For routed events, you can only use Handles to assign handlers for instances where that event exists in the instance members table. 不過,透過路由事件,父元素通常可以是來自子元素之事件的接聽程式,即使父元素的成員資料表中沒有該事件也一樣。However, with routed events in general, a parent element can be a listener for an event from child elements, even if the parent element does not have that event in its members table. 在屬性語法中,您可以透過 typename.membername 屬性格式來指定此動作,此格式會限定哪種類型實際上會定義您想要處理的事件。In attribute syntax, you can specify this through a typename.membername attribute form that qualifies which type actually defines the event you want to handle. 例如,父 Page (未定義 Click 事件)可以藉由指派格式為 Button.Click的屬性處理常式來接聽按鈕按一下事件。For instance, a parent Page (with no Click event defined) can listen for button-click events by assigning an attribute handler in the form Button.Click. 但是 Handles 不支援 typename.membername 格式,因為它必須支援衝突的 Instance.Event 格式。But Handles does not support the typename.membername form, because it must support a conflicting Instance.Event form. 如需詳細資訊,請參閱路由事件概觀For details, see Routed Events Overview.

Handles 無法附加要針對已經標示為已處理之事件叫用的處理常式。Handles cannot attach handlers that are invoked for events that are already marked handled. 相反地,您必須使用程式碼並呼叫 AddHandler(RoutedEvent, Delegate, Boolean)handledEventsToo 多載。Instead, you must use code and call the handledEventsToo overload of AddHandler(RoutedEvent, Delegate, Boolean).

注意

當您為 XAML 中的相同事件指定事件處理常式時,請勿在 Visual Basic 程式碼中使用 Handles 語法。Do not use the Handles syntax in Visual Basic code when you specify an event handler for the same event in XAML. 在這個情況下,會呼叫事件處理常式兩次。In this case, the event handler is called twice.

WPF 如何實作「Handles」功能How WPF Implements "Handles" Functionality

編譯 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) 頁面時,中繼檔案會宣告 Friend WithEvents 對頁面上具有 Name 屬性集(或已宣告x:Name指示詞)之每個元素的參考。When a Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) page is compiled, the intermediate file declares Friend WithEvents references to every element on the page that has a Name property set (or x:Name Directive declared). 每個具名執行個體可能都是可透過 Handles 指派給處理常式的元素。Each named instance is potentially an element that can be assigned to a handler through Handles.

注意

在 Visual Studio 中,IntelliSense 可以讓您完成哪些元素可供頁面中的 Handles 參考。Within Visual Studio, IntelliSense can show you completion for which elements are available for a Handles reference in a page. 不過,這可能需要採取一次編譯傳遞,讓中繼檔案可以填入所有的 Friends 參考。However, this might take one compile pass so that the intermediate file can populate all the Friends references.

請參閱See also