Files
Semi.Avalonia/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml.cs

47 lines
1.5 KiB
C#
Raw Normal View History

2024-12-20 23:38:53 +08:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
2024-12-20 20:56:52 +08:00
using Avalonia.Controls.Primitives;
2024-12-20 23:38:53 +08:00
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
2024-12-20 20:56:52 +08:00
using Avalonia.Threading;
2024-12-20 21:56:04 +08:00
using Semi.Avalonia.Demo.ViewModels;
2024-12-20 20:56:52 +08:00
namespace Semi.Avalonia.Demo.Pages;
public partial class IconDemo : UserControl
{
2024-12-20 23:38:53 +08:00
private IClipboard? _clipboard;
private WindowNotificationManager? _windowNotificationManager;
2024-12-20 20:56:52 +08:00
public IconDemo()
{
InitializeComponent();
2024-12-20 21:56:04 +08:00
this.DataContext = new IconDemoViewModel();
2024-12-20 20:56:52 +08:00
}
protected override async void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
2024-12-20 21:56:04 +08:00
var vm = this.DataContext as IconDemoViewModel;
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
2024-12-20 20:56:52 +08:00
}
2024-12-20 23:38:53 +08:00
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
var topLevel = TopLevel.GetTopLevel(this);
_windowNotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 };
_clipboard = topLevel?.Clipboard;
}
private async void Button_Clicked(object sender, RoutedEventArgs e)
{
if (_clipboard is null) return;
if (sender is not Button { DataContext: IconItem s }) return;
await _clipboard.SetTextAsync(s.ResourceKey);
_windowNotificationManager?.Show(
new Notification("Copied", s.ResourceKey),
NotificationType.Success);
}
2024-12-20 20:56:52 +08:00
}