From a054d17fd7eddeb469fb92a98ce23513d55bdc21 Mon Sep 17 00:00:00 2001 From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:28:13 +0800 Subject: [PATCH] fix: remove Manager OnDetachedFromVisualTree. --- demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs | 17 +++++++++++++++-- demo/Ursa.Demo/Pages/ToastDemo.axaml.cs | 17 +++++++++++++++-- demo/Ursa.Demo/ViewModels/MenuViewModel.cs | 4 ++-- .../Controls/NotificationShared/MessageCard.cs | 8 ++++---- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs b/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs index c4c0458..be0e00d 100644 --- a/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs @@ -1,5 +1,7 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Primitives; +using Avalonia.VisualTree; using Ursa.Controls; using Ursa.Demo.ViewModels; @@ -8,6 +10,7 @@ namespace Ursa.Demo.Pages; public partial class NotificationDemo : UserControl { private NotificationDemoViewModel _viewModel; + private TopLevel? _topLevel; public NotificationDemo() { @@ -19,7 +22,17 @@ public partial class NotificationDemo : UserControl protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); - var topLevel = TopLevel.GetTopLevel(this); - _viewModel.NotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 }; + _topLevel = TopLevel.GetTopLevel(this); + _viewModel.NotificationManager = new WindowNotificationManager(_topLevel) { MaxItems = 3 }; + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + var adorner = _topLevel.FindDescendantOfType()?.AdornerLayer; + if (adorner is not null && _viewModel.NotificationManager is not null) + { + adorner.Children.Remove(_viewModel.NotificationManager); + } } } \ No newline at end of file diff --git a/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs b/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs index 7e4f490..a39d32f 100644 --- a/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/ToastDemo.axaml.cs @@ -1,5 +1,7 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Primitives; +using Avalonia.VisualTree; using Ursa.Controls; using Ursa.Demo.ViewModels; @@ -8,6 +10,7 @@ namespace Ursa.Demo.Pages; public partial class ToastDemo : UserControl { private ToastDemoViewModel _viewModel; + private TopLevel? _topLevel; public ToastDemo() { @@ -19,7 +22,17 @@ public partial class ToastDemo : UserControl protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); - var topLevel = TopLevel.GetTopLevel(this); - _viewModel.ToastManager = new WindowToastManager(topLevel) { MaxItems = 3 }; + _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); + } } } \ No newline at end of file diff --git a/demo/Ursa.Demo/ViewModels/MenuViewModel.cs b/demo/Ursa.Demo/ViewModels/MenuViewModel.cs index a0ce383..7d4ab63 100644 --- a/demo/Ursa.Demo/ViewModels/MenuViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/MenuViewModel.cs @@ -37,7 +37,7 @@ public class MenuViewModel: ViewModelBase new() { MenuHeader = "Message Box", Key = MenuKeys.MenuKeyMessageBox }, new() { MenuHeader = "MultiComboBox", Key = MenuKeys.MenuKeyMultiComboBox, Status = "Updated" }, new() { MenuHeader = "Nav Menu", Key = MenuKeys.MenuKeyNavMenu }, - new() { MenuHeader = "Notification", Key = MenuKeys.MenuKeyNotification }, + new() { MenuHeader = "Notification", Key = MenuKeys.MenuKeyNotification, Status = "New"}, new() { MenuHeader = "Number Displayer", Key = MenuKeys.MenuKeyNumberDisplayer, Status = "New" }, new() { MenuHeader = "Numeric UpDown", Key = MenuKeys.MenuKeyNumericUpDown }, new() { MenuHeader = "NumPad", Key = MenuKeys.MenuKeyNumPad }, @@ -54,7 +54,7 @@ public class MenuViewModel: ViewModelBase new() { MenuHeader = "Timeline", Key = MenuKeys.MenuKeyTimeline }, new() { MenuHeader = "TreeComboBox", Key = MenuKeys.MenuKeyTreeComboBox }, new() { MenuHeader = "TwoTonePathIcon", Key = MenuKeys.MenuKeyTwoTonePathIcon}, - new() { MenuHeader = "Toast", Key = MenuKeys.MenuKeyToast }, + new() { MenuHeader = "Toast", Key = MenuKeys.MenuKeyToast, Status = "New"}, new() { MenuHeader = "ToolBar", Key = MenuKeys.MenuKeyToolBar }, new() { MenuHeader = "Time Box", Key = MenuKeys.MenuKeyTimeBox }, }; diff --git a/src/Ursa/Controls/NotificationShared/MessageCard.cs b/src/Ursa/Controls/NotificationShared/MessageCard.cs index 3400a45..3c0bd60 100644 --- a/src/Ursa/Controls/NotificationShared/MessageCard.cs +++ b/src/Ursa/Controls/NotificationShared/MessageCard.cs @@ -196,19 +196,19 @@ public abstract class MessageCard : ContentControl { switch (NotificationType) { - case Avalonia.Controls.Notifications.NotificationType.Error: + case NotificationType.Error: PseudoClasses.Add(PC_Error); break; - case Avalonia.Controls.Notifications.NotificationType.Information: + case NotificationType.Information: PseudoClasses.Add(PC_Information); break; - case Avalonia.Controls.Notifications.NotificationType.Success: + case NotificationType.Success: PseudoClasses.Add(PC_Success); break; - case Avalonia.Controls.Notifications.NotificationType.Warning: + case NotificationType.Warning: PseudoClasses.Add(PC_Warning); break; }