From c2e7a217abce666309e431327d5030d0ee6ee836 Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Fri, 28 Mar 2025 11:24:05 +0800 Subject: [PATCH] feat: simplify demo. --- .../Dialogs/CustomDemoDialog.axaml.cs | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/demo/Ursa.Demo/Dialogs/CustomDemoDialog.axaml.cs b/demo/Ursa.Demo/Dialogs/CustomDemoDialog.axaml.cs index c8353aa..7e5cd07 100644 --- a/demo/Ursa.Demo/Dialogs/CustomDemoDialog.axaml.cs +++ b/demo/Ursa.Demo/Dialogs/CustomDemoDialog.axaml.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Notifications; @@ -23,24 +24,14 @@ public partial class CustomDemoDialog : UserControl base.OnAttachedToVisualTree(e); _viewModel = this.DataContext as CustomDemoDialogViewModel; var visualLayerManager = this.FindAncestorOfType(); - if (visualLayerManager is not null && _viewModel is not null) - { - _viewModel.NotificationManager = new WindowNotificationManager(visualLayerManager) { MaxItems = 3 }; - _viewModel.ToastManager = new WindowToastManager(visualLayerManager) { MaxItems = 3 }; - } - - WindowNotificationManager.TryGetNotificationManager(visualLayerManager, out var manager); - if (manager is not null && _viewModel is not null) - { - Console.WriteLine(ReferenceEquals(_viewModel.NotificationManager, manager)); - manager.Position = NotificationPosition.TopCenter; - } - } - - protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) - { - base.OnDetachedFromVisualTree(e); - _viewModel?.NotificationManager?.Uninstall(); - _viewModel?.ToastManager?.Uninstall(); + if (_viewModel == null) return; + _viewModel.NotificationManager = + WindowNotificationManager.TryGetNotificationManager(visualLayerManager, out var notificationManager) + ? notificationManager + : new WindowNotificationManager(visualLayerManager) { MaxItems = 3 }; + _viewModel.ToastManager = WindowToastManager.TryGetToastManager(visualLayerManager, out var toastManager) + ? toastManager + : new WindowToastManager(visualLayerManager) { MaxItems = 3 }; + Debug.Assert(WindowNotificationManager.TryGetNotificationManager(visualLayerManager, out _)); } } \ No newline at end of file