feat: add VisualLayerManager ctor to Notification & Toast manager.

This commit is contained in:
Zhang Dian
2024-12-23 17:22:02 +08:00
parent 471840b574
commit b16a51ee4f
6 changed files with 82 additions and 15 deletions

View File

@@ -75,6 +75,14 @@
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="8">
<Button
Command="{Binding ShowNotificationCommand}"
Content="Notification"
Theme="{DynamicResource SolidButton}" />
<Button
Command="{Binding ShowToastCommand}"
Content="Toast"
Theme="{DynamicResource SolidButton}" />
<Button
Command="{Binding DialogCommand}"
Content="Dialog"
@@ -97,4 +105,4 @@
</StackPanel>
</Grid>
</UserControl>
</UserControl>

View File

@@ -1,13 +1,36 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Controls.Primitives;
using Avalonia.VisualTree;
using Ursa.Controls;
namespace Ursa.Demo.Dialogs;
public partial class CustomDemoDialog : UserControl
{
private CustomDemoDialogViewModel? _viewModel;
public CustomDemoDialog()
{
InitializeComponent();
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_viewModel = this.DataContext as CustomDemoDialogViewModel;
var visualLayerManager = this.FindAncestorOfType<VisualLayerManager>();
if (visualLayerManager is not null && _viewModel is not null)
{
_viewModel.NotificationManager = new WindowNotificationManager(visualLayerManager) { MaxItems = 3 };
_viewModel.ToastManager = new WindowToastManager(visualLayerManager) { MaxItems = 3 };
}
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
_viewModel?.NotificationManager?.Uninstall();
_viewModel?.ToastManager?.Uninstall();
}
}

View File

@@ -15,6 +15,8 @@ public partial class CustomDemoDialogViewModel : ObservableObject, IDialogContex
[ObservableProperty] private string? _department;
[ObservableProperty] private string? _owner;
[ObservableProperty] private string? _target;
public WindowNotificationManager? NotificationManager { get; set; }
public WindowToastManager? ToastManager { get; set; }
public CustomDemoDialogViewModel()
{
@@ -36,12 +38,11 @@ public partial class CustomDemoDialogViewModel : ObservableObject, IDialogContex
}
public event EventHandler<object?>? RequestClose;
public ICommand OKCommand { get; set; }
public ICommand CancelCommand { get; set; }
public ICommand DialogCommand { get; set; }
private void OK()
{
RequestClose?.Invoke(this, true);
@@ -51,9 +52,22 @@ public partial class CustomDemoDialogViewModel : ObservableObject, IDialogContex
{
RequestClose?.Invoke(this, false);
}
private async Task ShowDialog()
{
await OverlayDialog.ShowCustomModal<CustomDemoDialog, CustomDemoDialogViewModel, bool>(new CustomDemoDialogViewModel());
await OverlayDialog.ShowCustomModal<CustomDemoDialog, CustomDemoDialogViewModel, bool>(
new CustomDemoDialogViewModel());
}
[RelayCommand]
private void ShowToast(object obj)
{
ToastManager?.Show("This is a Toast message");
}
[RelayCommand]
private void ShowNotification(object obj)
{
NotificationManager?.Show("This is a Notification message");
}
}