XamlDirect.AddEventHandler XamlDirect.AddEventHandler XamlDirect.AddEventHandler XamlDirect.AddEventHandler Method

Definition

Overloads

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object)

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

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean)

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

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object)

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

public : void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, Platform::Object handler)
void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, winrt::Windows::Foundation::IInspectable handler) const;
public void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, Object handler)
Public Sub AddEventHandler(xamlDirectObject As IXamlDirectObject, eventIndex As XamlEventIndex, handler As Object)

Parameters

xamlDirectObject
IXamlDirectObject IXamlDirectObject

A reference to the current IXamlDirectObject.

eventIndex
XamlEventIndex XamlEventIndex

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

handler
Object Object

A reference to the specified handler implementation.

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

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.

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean) AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean)

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

public : void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, Platform::Object handler, bool handledEventsToo)
void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, winrt::Windows::Foundation::IInspectable handler, bool handledEventsToo) const;
public void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, Object handler, Boolean handledEventsToo)
Public Sub AddEventHandler(xamlDirectObject As IXamlDirectObject, eventIndex As XamlEventIndex, handler As Object, handledEventsToo As Boolean)

Parameters

xamlDirectObject
IXamlDirectObject IXamlDirectObject

A reference to the current IXamlDirectObject.

eventIndex
XamlEventIndex XamlEventIndex

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

handler
Object Object

A reference to the specified handler implementation.

handledEventsToo
Boolean Boolean

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.

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, true);
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, 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.