Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shweaver-msft/loadedunloadedevents
FrameworkElement Load/Unload event propagation normalization
https://github.com/shweaver-msft/loadedunloadedevents
uwp xaml
Last synced: 24 days ago
JSON representation
FrameworkElement Load/Unload event propagation normalization
- Host: GitHub
- URL: https://github.com/shweaver-msft/loadedunloadedevents
- Owner: shweaver-MSFT
- License: mit
- Created: 2019-09-20T00:08:17.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-20T16:17:04.000Z (over 5 years ago)
- Last Synced: 2024-11-13T05:22:20.471Z (3 months ago)
- Topics: uwp, xaml
- Language: C#
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LoadedUnloadedEvents
In UWP XAML, it is possible for the Loaded and Unloaded events of a FrameworkElement to propagate in a different order than they occurred.
A common instance is when an element is disconnected from the VisualTree and re-inserted somewhere else.
Without additional checks, any code that relies on those events bubbling in a consistent order will be at risk of breaking.This solution offers a simple workaround, in the form of the `ElementLoadNormalizer.cs`
Simply add the `ElementLoadNormalizer` to your application and adopt the pattern below to ensure the correct processing order:
```
public MainPage()
{
InitializeComponent();myElement.Loaded += OnLoaded;
myElement.Unloaded += OnUnloaded;// Wire up the loading event extensions
myElement.ExtendLoadingEvents();
}private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement element)
{
var lastLoadedArgs = element.GetLastLoadedEventArgs();
if (lastLoadedArgs != null)
{
HandleLoaded(sender, lastLoadedArgs);
}
HandleUnloaded(sender, e);
}
}private void OnLoaded(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement element)
{
HandleLoaded(sender, e);
var lastUnloadedArgs = element.GetLastUnloadedEventArgs();
if (lastUnloadedArgs != null)
{
HandleUnloaded(sender, lastUnloadedArgs);
}
}
}private void HandleUnloaded(object sender, RoutedEventArgs e) { ... }
private void HandleLoaded(object sender, RoutedEventArgs e) { ... }
```