feat: simplify VM initialization.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
@@ -16,10 +15,10 @@ public partial class PopConfirmDemo : UserControl
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
if (this.DataContext is not PopConfirmDemoViewModel vm) return;
|
||||
var manager = WindowToastManager.TryGetToastManager(TopLevel.GetTopLevel(this), out var m)
|
||||
? m
|
||||
: new WindowToastManager(TopLevel.GetTopLevel(this));
|
||||
vm.ToastManager = manager;
|
||||
if (DataContext is not PopConfirmDemoViewModel vm) return;
|
||||
var topLevel = TopLevel.GetTopLevel(this);
|
||||
vm.ToastManager = WindowToastManager.TryGetToastManager(topLevel, out var manager)
|
||||
? manager
|
||||
: new WindowToastManager(topLevel);
|
||||
}
|
||||
}
|
||||
@@ -7,25 +7,24 @@ namespace Ursa.Demo.Pages;
|
||||
|
||||
public partial class ToastDemo : UserControl
|
||||
{
|
||||
private ToastDemoViewModel _viewModel;
|
||||
private ToastDemoViewModel? _viewModel;
|
||||
|
||||
public ToastDemo()
|
||||
{
|
||||
InitializeComponent();
|
||||
_viewModel = new ToastDemoViewModel();
|
||||
DataContext = _viewModel;
|
||||
}
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
var topLevel = TopLevel.GetTopLevel(this);
|
||||
_viewModel.ToastManager = new WindowToastManager(topLevel) { MaxItems = 3 };
|
||||
if (DataContext is not ToastDemoViewModel vm) return;
|
||||
_viewModel = vm;
|
||||
_viewModel.ToastManager = new WindowToastManager(TopLevel.GetTopLevel(this)) { MaxItems = 3 };
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnDetachedFromVisualTree(e);
|
||||
_viewModel.ToastManager?.Uninstall();
|
||||
_viewModel?.ToastManager?.Uninstall();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user