doc/articles/features/routed-events.md
Per the WinUI contract, Uno Platform provides support for routed events, events which are 'bubbled' from a child object to each of its successive parents in the XAML object tree. In most cases, you can expect routed events to behave the same way on Windows and non-Windows platforms.
This article covers some of the finer technical details of Uno Platform's routed events implementation that may be relevant in advanced scenarios, eg those involving custom native (non-WinUI) views in your visual tree.
[1]---------------------+
| An event is fired |
+--------+--------------+
|
[2]------v--------------+
| Event is dispatched |
| to corresponding | [12]
| element | ^
+-------yes-------------+ |
| [11]---no--------------+
|<---[13]-raise on parent---yes A parent is |
| | defined? |
[3]------v--------------+ | |
| Any local handlers? no--------+ +-------^--------------+
+-------yes-------------+ | |
| | [10]----+--------------+
[4]------v--------------+ | | Event is bubbling |
| Invoke local handlers | | | to parent in <--+
+--------+--------------+ | | managed code (Uno) | |
| | +-------^--------------+ |
[5]------v--------------+ | | |
| Is the event handled | v [6]-----no-------------+ |
| by local handlers? no------------>| Event is coming from | |
+-------yes-------------+ | platform? | |
| +------yes-------------+ |
[9]------v--------------+ | |
| Any parent interested | [7]-----v--------------+ |
| by this event? yes-+ | Is the event | |
+-------no--------------+ | | bubbling natively? no-+
| | +------yes-------------+
[12]-----v--------------+ | |
| Processing finished | v [8]-----v--------------+
| for this event. | [10] | Event is returned |
+-----------------------+ | for native |
| bubbling in platform |
+----------------------+
AddEventHandler(<evt>, <handler>, handledEventsToo: true)).handledEventsToo: true).A special property named EventsBubblingInManagedCode is used to determine how properties are propagated through
the platform. This value of this property is inherited in the visual tree.
This value is set to RoutedEventFlag.None by default, so all routed events are bubbling natively by default
(except for those which can't bubble natively like LostFocus on iOS or KeyDown on Android).
Bubbling natively will improve interoperability with native UI elements in the visual tree by letting the platform propagate the events. But this will cause more interop between managed and unmanaged code.
You can control which events are bubbling in managed code by using the EventsBubblingInManagedCode
dependency property. The value of this property is inherited to children. Example:
// Make sure PointerPressed and PointerReleased are always bubbling in
// managed code when they are originating from myControl and its children.
myControl.EventsBubblingInManagedCode =
RoutedEventFlag.PointerPressed | RoutedEventFlag.PointerReleased;
When using a managed bubbling for a routed event, you'll have a the following limitations:
handledEventsToo: true won't receive events if they are consumed
(marked as handled) in platform components. If it's a problem, you must switch to managed bubbling
for those events. If the event is consumed before reaching the first managed handler, you must
add a handler in one one the children to give Uno a way to intercept the event and make it bubble
in managed code.handledEventsToo: true when possible.Due to serious differences between various platforms, some compromises were done during the implementation of RoutedEvents:
OriginalSource might not be accurate on RoutedEventArgsIn some cases/events, it's possible that the OriginalSource property of the RoutedEventArgs is null
or referencing the element where the event crossed the native-to-managed boundary.
This property is however always accurate for "Pointers", "Manipulation", "Gesture", and "Drag and drop" events.
Handled to false won't behave like in UWPThere's a strange behavior in UWP where you can switch back the event.Handle to false when
intercepted by a handler with handledEventsToo: true. In UWP, the event will continue to bubble normally.
Trying to do this in Uno can lead to unreliable results.