From 850339f32437a012fc7bb4736c0eeacba78a91a4 Mon Sep 17 00:00:00 2001 From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:48:26 +0800 Subject: [PATCH] feat: enhancement. --- demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs | 7 ++----- demo/Ursa.Demo/Pages/ToastDemo.axaml.cs | 13 +++---------- .../NotificationShared/WindowMessageManager.cs | 2 +- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs b/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs index 6fc84bf..31d0e78 100644 --- a/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs @@ -1,8 +1,6 @@ using Avalonia; using Avalonia.Controls; -using Avalonia.Controls.Primitives; using Avalonia.LogicalTree; -using Avalonia.VisualTree; using Ursa.Controls; using Ursa.Demo.ViewModels; @@ -11,7 +9,6 @@ namespace Ursa.Demo.Pages; public partial class NotificationDemo : UserControl { private NotificationDemoViewModel _viewModel; - private TopLevel? _topLevel; public NotificationDemo() { @@ -23,8 +20,8 @@ public partial class NotificationDemo : UserControl protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); - _topLevel = TopLevel.GetTopLevel(this); - _viewModel.NotificationManager = new WindowNotificationManager(_topLevel) { MaxItems = 3 }; + var topLevel = TopLevel.GetTopLevel(this); + _viewModel.NotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 }; } protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) diff --git a/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs b/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs index a39d32f..714eeb9 100644 --- a/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs @@ -1,7 +1,5 @@ using Avalonia; using Avalonia.Controls; -using Avalonia.Controls.Primitives; -using Avalonia.VisualTree; using Ursa.Controls; using Ursa.Demo.ViewModels; @@ -10,7 +8,6 @@ namespace Ursa.Demo.Pages; public partial class ToastDemo : UserControl { private ToastDemoViewModel _viewModel; - private TopLevel? _topLevel; public ToastDemo() { @@ -22,17 +19,13 @@ public partial class ToastDemo : UserControl protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); - _topLevel = TopLevel.GetTopLevel(this); - _viewModel.ToastManager = new WindowToastManager(_topLevel) { MaxItems = 3 }; + var topLevel = TopLevel.GetTopLevel(this); + _viewModel.ToastManager = new WindowToastManager(topLevel) { MaxItems = 3 }; } protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { base.OnDetachedFromVisualTree(e); - var adorner = _topLevel.FindDescendantOfType()?.AdornerLayer; - if (adorner is not null && _viewModel.ToastManager is not null) - { - adorner.Children.Remove(_viewModel.ToastManager); - } + _viewModel.ToastManager?.Uninstall(); } } \ No newline at end of file diff --git a/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs b/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs index 40b3e25..61f5f05 100644 --- a/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs +++ b/src/Ursa/Controls/NotificationShared/WindowMessageManager.cs @@ -66,7 +66,7 @@ public abstract class WindowMessageManager : TemplatedControl public virtual void Uninstall() { - if(Parent is AdornerLayer adornerLayer) + if (Parent is AdornerLayer adornerLayer) { adornerLayer.Children.Remove(this); AdornerLayer.SetAdornedElement(this, null);