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
{
private NotificationDemoViewModel _viewModel;
public NotificationDemo()
{
InitializeComponent();
_viewModel = new NotificationDemoViewModel();
DataContext = _viewModel;
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (DataContext is not NotificationDemoViewModel vm) return;
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel is null)
return;
_viewModel.NotificationManager = WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager)
vm.NotificationManager = WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager)
? manager
: new WindowNotificationManager(topLevel);
}