feat: support uninstall.

This commit is contained in:
rabbitism
2024-09-12 18:38:27 +08:00
parent a054d17fd7
commit ea385ee194
2 changed files with 13 additions and 7 deletions

View File

@@ -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<VisualLayerManager>()?.AdornerLayer;
if (adorner is not null && _viewModel.NotificationManager is not null)
{
adorner.Children.Remove(_viewModel.NotificationManager);
}
base.OnDetachedFromLogicalTree(e);
_viewModel.NotificationManager?.Uninstall();
}
}

View File

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