Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 2.71 KB

xamldirect_addeventhandler_950477410.md

File metadata and controls

68 lines (49 loc) · 2.71 KB
-api-id -api-type ms.custom
M:Windows.UI.Xaml.Core.Direct.XamlDirect.AddEventHandler(Windows.UI.Xaml.Core.Direct.IXamlDirectObject,Windows.UI.Xaml.Core.Direct.XamlEventIndex,System.Object)
winrt method
RS5

Windows.UI.Xaml.Core.Direct.XamlDirect.AddEventHandler

-description

Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirectObject.

-parameters

-param xamlDirectObject

A reference to the current IXamlDirectObject.

-param eventIndex

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

-param handler

A reference to the specified handler implementation.

-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-also

XamlDirect.AddEventHandler, XamlDirect.RemoveEventHandler

-examples

The following example shows how to add/modify the PointerEntered event through a specific instance of PointerEventHandler on a ToggleSwitch control from its IXamlDirectObject instance.

XamlDirect xd = XamlDirect.GetDefault();

IXamlDirectObject 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);
XamlDirect^ xd = XamlDirect::GetDefault();

IXamlDirectObject^ 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);