feat: simplify VM initialization.

This commit is contained in:
Zhang Dian
2025-06-04 17:12:54 +08:00
parent 5d5c439cb0
commit 447629170b
3 changed files with 12 additions and 19 deletions

View File

@@ -7,22 +7,17 @@ namespace Ursa.Demo.Pages;
public partial class NotificationDemo : UserControl public partial class NotificationDemo : UserControl
{ {
private NotificationDemoViewModel _viewModel;
public NotificationDemo() public NotificationDemo()
{ {
InitializeComponent(); InitializeComponent();
_viewModel = new NotificationDemoViewModel();
DataContext = _viewModel;
} }
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
if (DataContext is not NotificationDemoViewModel vm) return;
var topLevel = TopLevel.GetTopLevel(this); var topLevel = TopLevel.GetTopLevel(this);
if (topLevel is null) vm.NotificationManager = WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager)
return;
_viewModel.NotificationManager = WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager)
? manager ? manager
: new WindowNotificationManager(topLevel); : new WindowNotificationManager(topLevel);
} }

View File

@@ -1,6 +1,5 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Ursa.Controls; using Ursa.Controls;
using Ursa.Demo.ViewModels; using Ursa.Demo.ViewModels;
@@ -16,10 +15,10 @@ public partial class PopConfirmDemo : UserControl
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
if (this.DataContext is not PopConfirmDemoViewModel vm) return; if (DataContext is not PopConfirmDemoViewModel vm) return;
var manager = WindowToastManager.TryGetToastManager(TopLevel.GetTopLevel(this), out var m) var topLevel = TopLevel.GetTopLevel(this);
? m vm.ToastManager = WindowToastManager.TryGetToastManager(topLevel, out var manager)
: new WindowToastManager(TopLevel.GetTopLevel(this)); ? manager
vm.ToastManager = manager; : new WindowToastManager(topLevel);
} }
} }

View File

@@ -7,25 +7,24 @@ namespace Ursa.Demo.Pages;
public partial class ToastDemo : UserControl public partial class ToastDemo : UserControl
{ {
private ToastDemoViewModel _viewModel; private ToastDemoViewModel? _viewModel;
public ToastDemo() public ToastDemo()
{ {
InitializeComponent(); InitializeComponent();
_viewModel = new ToastDemoViewModel();
DataContext = _viewModel;
} }
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
var topLevel = TopLevel.GetTopLevel(this); if (DataContext is not ToastDemoViewModel vm) return;
_viewModel.ToastManager = new WindowToastManager(topLevel) { MaxItems = 3 }; _viewModel = vm;
_viewModel.ToastManager = new WindowToastManager(TopLevel.GetTopLevel(this)) { MaxItems = 3 };
} }
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnDetachedFromVisualTree(e); base.OnDetachedFromVisualTree(e);
_viewModel.ToastManager?.Uninstall(); _viewModel?.ToastManager?.Uninstall();
} }
} }