Files
ShrlAlgoToolkit/AntDesignWPF/Controls/Notification/NotificationView.xaml.cs
2025-07-31 20:12:24 +08:00

72 lines
2.0 KiB
C#

using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace AntDesignWPF.Controls
{
/// <summary>
/// NotificationWindow.xaml 的交互逻辑
/// </summary>
public partial class NotificationView : Window
{
//public NotificationWindow()
//{
// InitializeComponent();
//}
private readonly DispatcherTimer _closeTimer;
// Dependency Properties to allow data binding
public string Title { get; set; }
public string Message { get; set; }
public NotificationType Type { get; set; }
public TimeSpan Duration { get; set; }
public NotificationView(string title, string message, NotificationType type, TimeSpan duration)
{
InitializeComponent();
Title = title;
Message = message;
Type = type;
Duration = duration;
DataContext = this;
if (duration > TimeSpan.Zero)
{
_closeTimer = new DispatcherTimer { Interval = duration };
_closeTimer.Tick += (s, e) => CloseAnimation();
_closeTimer.Start();
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
_closeTimer?.Stop();
CloseAnimation();
}
private void CloseAnimation()
{
var anim = new DoubleAnimation(0, TimeSpan.FromSeconds(0.3));
anim.Completed += (s, _) => Close();
BeginAnimation(OpacityProperty, anim);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Start open animation
var anim = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(0.3));
BeginAnimation(OpacityProperty, anim);
}
}
// Enum to define notification types
public enum NotificationType
{
Info,
Success,
Warning,
Error
}
}