Share via


IXamlDirect.AddEventHandler Method

Definition

Overloads

AddEventHandler(Object, XamlEventIndex, Object)

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

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

AddEventHandler(Object, XamlEventIndex, Object, Boolean)

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).

AddEventHandler(Object, XamlEventIndex, Object)

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

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

public:
 void AddEventHandler(Platform::Object ^ xamlDirectObject, XamlEventIndex eventIndex, Platform::Object ^ handler);
/// [Windows.Foundation.Metadata.Overload("AddEventHandler2")]
void AddEventHandler(IInspectable const& xamlDirectObject, XamlEventIndex const& eventIndex, IInspectable const& handler);
[Windows.Foundation.Metadata.Overload("AddEventHandler2")]
public void AddEventHandler(object xamlDirectObject, XamlEventIndex eventIndex, object handler);
Public Sub AddEventHandler (xamlDirectObject As Object, eventIndex As XamlEventIndex, handler As Object)

Parameters

xamlDirectObject
Object

Platform::Object

IInspectable

A reference to the object receiving the event handler.

eventIndex
XamlEventIndex

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

handler
Object

Platform::Object

IInspectable

A reference to the specified handler implementation.

Attributes

Examples

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

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

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

Applies to

AddEventHandler(Object, XamlEventIndex, Object, Boolean)

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).

public:
 void AddEventHandler(Platform::Object ^ xamlDirectObject, XamlEventIndex eventIndex, Platform::Object ^ handler, bool handledEventsToo);
/// [Windows.Foundation.Metadata.Overload("AddEventHandler1")]
void AddEventHandler(IInspectable const& xamlDirectObject, XamlEventIndex const& eventIndex, IInspectable const& handler, bool const& handledEventsToo);
[Windows.Foundation.Metadata.Overload("AddEventHandler1")]
public void AddEventHandler(object xamlDirectObject, XamlEventIndex eventIndex, object handler, bool handledEventsToo);
Public Sub AddEventHandler (xamlDirectObject As Object, eventIndex As XamlEventIndex, handler As Object, handledEventsToo As Boolean)

Parameters

xamlDirectObject
Object

Platform::Object

IInspectable

A reference to the current IXamlDirect.

eventIndex
XamlEventIndex

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

handler
Object

Platform::Object

IInspectable

A reference to the specified handler implementation.

handledEventsToo
Boolean

bool

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.

Attributes

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

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

Applies to