Skip to content

Latest commit

 

History

History
83 lines (53 loc) · 3.59 KB

ixamldirect_addeventhandler_1505961231.md

File metadata and controls

83 lines (53 loc) · 3.59 KB
-api-id -api-type
M:Microsoft.UI.Xaml.Core.Direct.IXamlDirect.AddEventHandler(System.Object,Microsoft.UI.Xaml.Core.Direct.XamlEventIndex,System.Object,System.Boolean)
winrt method

Microsoft.UI.Xaml.Core.Direct.IXamlDirect.AddEventHandler(System.Object,Microsoft.UI.Xaml.Core.Direct.XamlEventIndex,System.Object,System.Boolean)

-description

Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirect. Specify handledEventsToo as true to have the provided handler be invoked even if the event is handled elsewhere.

This documentation applies to WinUI 2 for UWP (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

-parameters

-param xamlDirectObject

A reference to the current IXamlDirect.

-param eventIndex

An identifier for the event to be handled specified through XamlEventIndex enum.

-param handler

A reference to the specified handler implementation.

-param handledEventsToo

true to register the handler such that it is invoked even when the routed event is marked handled in its event data.

false to register the handler with the default condition that it will not be invoked if the routed event is already marked handled.

The default is false and the parameter is optional.

-remarks

AddEventHandler can only be used to add event handlers for the events supported by the XamlEventIndex enumeration. You can use this method to add handlers to routed as well non-routed events supported by the xaml object.

See UIElement.AddHandler for when to use handledEventsToo and the restrictions around the same. For non-routed events, the handleEventsToo flag is completely ignored.

-see-also

Microsoft.UI.Xaml.Core.Direct.IXamlDirect.AddEventHandler(System.Object,Microsoft.UI.Xaml.Core.Direct.XamlEventIndex,System.Object), Microsoft.UI.Xaml.Core.Direct.IXamlDirect.RemoveEventHandler(System.Object,Microsoft.UI.Xaml.Core.Direct.XamlEventIndex,System.Object), UIElement.AddHandler, UIElement.RemoveHandler

-examples

XamlDirect xd = XamlDirect.GetDefault();

IXamlDirect toggleSwitch = xd.CreateInstance(XamlTypeIndex.ToggleSwitch);

PointerEventHandler toggleSwitchPointerHandler = new PointerEventHandler((sender, args) =>
{
    if (sender is ToggleSwitch)
    {
        ((ToggleSwitch)sender).IsOn = !((ToggleSwitch)sender).IsOn;
    }
});

xd.AddEventHandler(toggleSwitch, XamlEventIndex.UIElement_PointerEntered, toggleSwitchPointerHandler, true);
XamlDirect^ xd = XamlDirect::GetDefault();

IXamlDirect^ toggleSwitch = xd->CreateInstance(XamlTypeIndex::ToggleSwitch);

PointerEventHandler^ toggleSwitchPointerHandler = ref new PointerEventHandler([&](Platform::Object^ sender, PointerRoutedEventArgs^ args)
{
    ToggleSwitch^ ts = dynamic_cast<ToggleSwitch^>(sender);
    if (nullptr != ts)
    {
        ts->IsOn = !ts->IsOn;
    }
});

xd->AddEventHandler(toggleSwitch, XamlEventIndex::UIElement_PointerEntered, toggleSwitchPointerHandler, true);