diff --git a/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs b/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs index be0e00d..6fc84bf 100644 --- a/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs @@ -1,6 +1,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; +using Avalonia.LogicalTree; using Avalonia.VisualTree; using Ursa.Controls; using Ursa.Demo.ViewModels; @@ -26,13 +27,9 @@ public partial class NotificationDemo : UserControl _viewModel.NotificationManager = new WindowNotificationManager(_topLevel) { MaxItems = 3 }; } - protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) { - base.OnDetachedFromVisualTree(e); - var adorner = _topLevel.FindDescendantOfType()?.AdornerLayer; - if (adorner is not null && _viewModel.NotificationManager is not null) - { - adorner.Children.Remove(_viewModel.NotificationManager); - } + base.OnDetachedFromLogicalTree(e); + _viewModel.NotificationManager?.Uninstall(); } } \ No newline at end of file diff --git a/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs b/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs index e79e03e..40b3e25 100644 --- a/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs +++ b/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs @@ -64,6 +64,15 @@ public abstract class WindowMessageManager : TemplatedControl } } + public virtual void Uninstall() + { + if(Parent is AdornerLayer adornerLayer) + { + adornerLayer.Children.Remove(this); + AdornerLayer.SetAdornedElement(this, null); + } + } + protected void TopLevelOnTemplateApplied(object? sender, TemplateAppliedEventArgs e) { if (Parent is AdornerLayer adornerLayer)