feat: ensure notification manager is initialized.

This commit is contained in:
Dong Bin
2025-03-28 15:41:16 +08:00
parent d30de5e7cd
commit 243be53d04
2 changed files with 10 additions and 11 deletions

View File

@@ -20,11 +20,10 @@ public partial class NotificationDemo : UserControl
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
var topLevel = TopLevel.GetTopLevel(this); var topLevel = TopLevel.GetTopLevel(this);
if (topLevel is null)
WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager); return;
if (manager is not null) _viewModel.NotificationManager = WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager)
{ ? manager
_viewModel.NotificationManager = manager; : new WindowNotificationManager(topLevel);
}
} }
} }

View File

@@ -19,10 +19,10 @@ public partial class MainView : UserControl
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
_viewModel = DataContext as MainViewViewModel; _viewModel = DataContext as MainViewViewModel;
var topLevel = TopLevel.GetTopLevel(this); var topLevel = TopLevel.GetTopLevel(this);
WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager); if (topLevel is null || _viewModel is null)
if (manager is not null && _viewModel is not null) return;
{ _viewModel.NotificationManager = WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager)
_viewModel.NotificationManager = manager; ? manager
} : new WindowNotificationManager(topLevel);
} }
} }