namespace ZwWhForXhhk.Net.View
{
/// <summary>
/// WpfRightNotify.xaml 的交互逻辑
/// </summary>
public partial class WpfRightNotify : Window
{
private DispatcherTimer timer;
public WpfRightNotify(string message, ToastType type, int durationSeconds)
{
InitializeComponent();
MessageText.AppendText(message);
SetStyle(type);
Loaded += (s, e) =>
{
var area = SystemParameters.WorkArea;
Left = area.Right - Width/* - 10*/;
Top = area.Bottom - Height/* - 10*/ - ToastManager.GetYOffset(this);
ShowAnimation(durationSeconds);
};
}
private void SetStyle(ToastType type)
{
switch (type)
{
case ToastType.Success:
RootBorder.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.SeaGreen);
IconText.Text = "成功";
IconImage.Kind = (MaterialDesignThemes.Wpf.PackIconKind)Enum.Parse(typeof(MaterialDesignThemes.Wpf.PackIconKind), "CheckBold");
IconImage.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.SeaGreen);
IconImage.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DarkGreen);
break;
case ToastType.Warning:
RootBorder.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Orange);
IconText.Text = "警告";
IconImage.Kind = (MaterialDesignThemes.Wpf.PackIconKind)Enum.Parse(typeof(MaterialDesignThemes.Wpf.PackIconKind), "Alert");
IconImage.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Orange);
IconImage.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DarkOrange);
break;
case ToastType.Error:
RootBorder.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.IndianRed);
IconText.Text = "错误";
IconImage.Kind = (MaterialDesignThemes.Wpf.PackIconKind)Enum.Parse(typeof(MaterialDesignThemes.Wpf.PackIconKind), "Close");
IconImage.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.IndianRed);
IconImage.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DarkRed);
break;
default:
RootBorder.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DimGray);
IconText.Text = "信息";
IconImage.Kind = (MaterialDesignThemes.Wpf.PackIconKind)Enum.Parse(typeof(MaterialDesignThemes.Wpf.PackIconKind), "InformationVariantBoxOutline");
IconImage.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DimGray);
IconImage.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DarkGray);
break;
}
}
private void ShowAnimation(int durationSeconds)
{
//var fadeIn = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(300));
double right = System.Windows.SystemParameters.WorkArea.Right;//工作区最右边的值
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));//NotifyTimeSpan是自己定义的一个int型变量,用来设置动画的持续时间
animation.From = right;
animation.To = right - 300;//设定通知从右往左弹出
BeginAnimation(OpacityProperty, animation);
timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(durationSeconds) };
timer.Tick += (s, e) =>
{
timer.Stop();
CloseAnimation();
};
timer.Start();
}
private void CloseAnimation()
{
var fadeOut = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(300));
fadeOut.Completed += (s, e) =>
{
Close();
ToastManager.RemoveToast(this);
};
BeginAnimation(OpacityProperty, fadeOut);
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
ToastManager.RemoveToast(this);
//System.Windows.MessageBox.Show("关闭");
}
}
public enum ToastType
{
Info,
Success,
Warning,
Error
}
public static class ToastManager
{
private static List<WpfRightNotify> toasts = new List<WpfRightNotify>();
public static void ShowToast(string message, ToastType type = ToastType.Info, int durationSeconds = 3)
{
var toast = new WpfRightNotify(message, type, durationSeconds);
toasts.Add(toast);
toast.Show();
}
public static double GetYOffset(WpfRightNotify current)
{
double offset = 0;
foreach (var toast in toasts)
{
if (toast == current) break;
offset += toast.Height + 10;
}
return offset;
}
public static void RemoveToast(WpfRightNotify toast)
{
toasts.Remove(toast);
}
}
}