-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 |
Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirectObject.
A reference to the current IXamlDirectObject.
An identifier for the event to be handled specified through XamlEventIndex enum.
A reference to the specified handler implementation.
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.
XamlDirect.AddEventHandler, XamlDirect.RemoveEventHandler
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);