using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PDF_Master.CustomControl { /// /// 渐隐提示控件 /// public partial class ToastControl : UserControl { /// /// 这种小型组件,主要为UI显示效果的,就不用VM来处理了 /// public ToastControl() { InitializeComponent(); } public CornerRadius CornerRadius { get { return (CornerRadius)GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } } public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ToastControl), new PropertyMetadata(null)); public new Brush Background { get { return (Brush)GetValue(BackgroundProperty); } set { SetValue(BackgroundProperty, value); } } public static new readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(ToastControl), new PropertyMetadata(Brushes.White)); public string StringContent { get { return (string)GetValue(StringContentProperty); } set { SetValue(StringContentProperty, value); } } public static readonly DependencyProperty StringContentProperty = DependencyProperty.Register("StringContent", typeof(string), typeof(ToastControl), new PropertyMetadata("")); public Duration Duration { get { return (Duration)GetValue(DurationProperty); } set { SetValue(DurationProperty, value); } } public static readonly DependencyProperty DurationProperty = DependencyProperty.Register("Duration", typeof(Duration), typeof(ToastControl), new PropertyMetadata(new Duration(TimeSpan.FromSeconds(0)))); public TimeSpan BeginTime { get { return (TimeSpan)GetValue(BeginTimeProperty); } set { SetValue(BeginTimeProperty, value); } } public static readonly DependencyProperty BeginTimeProperty = DependencyProperty.Register("BeginTime", typeof(TimeSpan), typeof(ToastControl), new PropertyMetadata(TimeSpan.FromSeconds(0))); private void DoubleAnimation_Completed(object sender, EventArgs e) { //缺少关闭逻辑导致没有及时关闭动画,显示看起来比较卡顿 this.BeginAnimation(UserControl.OpacityProperty, null); this.Visibility = Visibility.Collapsed; this.Opacity = 1; } private void Toast_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if((bool)e.NewValue) { DoubleAnimation doubleAnimation = new DoubleAnimation(); doubleAnimation.Completed += DoubleAnimation_Completed; doubleAnimation.From = 1; doubleAnimation.To = 0; doubleAnimation.BeginTime = BeginTime; doubleAnimation.Duration = Duration; this.BeginAnimation(UserControl.OpacityProperty, doubleAnimation); } } } }