ToastControl.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Animation;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace PDF_Master.CustomControl
  19. {
  20. /// <summary>
  21. /// 渐隐提示控件
  22. /// </summary>
  23. public partial class ToastControl : UserControl
  24. {
  25. /// <summary>
  26. /// 这种小型组件,主要为UI显示效果的,就不用VM来处理了
  27. /// </summary>
  28. public ToastControl()
  29. {
  30. InitializeComponent();
  31. }
  32. public CornerRadius CornerRadius
  33. {
  34. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  35. set { SetValue(CornerRadiusProperty, value); }
  36. }
  37. public static readonly DependencyProperty CornerRadiusProperty =
  38. DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ToastControl), new PropertyMetadata(null));
  39. public new Brush Background
  40. {
  41. get { return (Brush)GetValue(BackgroundProperty); }
  42. set { SetValue(BackgroundProperty, value); }
  43. }
  44. public static new readonly DependencyProperty BackgroundProperty =
  45. DependencyProperty.Register("Background", typeof(Brush), typeof(ToastControl), new PropertyMetadata(Brushes.White));
  46. public string StringContent
  47. {
  48. get { return (string)GetValue(StringContentProperty); }
  49. set { SetValue(StringContentProperty, value); }
  50. }
  51. public static readonly DependencyProperty StringContentProperty =
  52. DependencyProperty.Register("StringContent", typeof(string), typeof(ToastControl), new PropertyMetadata(""));
  53. public Duration Duration
  54. {
  55. get { return (Duration)GetValue(DurationProperty); }
  56. set { SetValue(DurationProperty, value); }
  57. }
  58. public static readonly DependencyProperty DurationProperty =
  59. DependencyProperty.Register("Duration", typeof(Duration), typeof(ToastControl), new PropertyMetadata(new Duration(TimeSpan.FromSeconds(0))));
  60. public TimeSpan BeginTime
  61. {
  62. get { return (TimeSpan)GetValue(BeginTimeProperty); }
  63. set { SetValue(BeginTimeProperty, value); }
  64. }
  65. public static readonly DependencyProperty BeginTimeProperty =
  66. DependencyProperty.Register("BeginTime", typeof(TimeSpan), typeof(ToastControl), new PropertyMetadata(TimeSpan.FromSeconds(0)));
  67. private void DoubleAnimation_Completed(object sender, EventArgs e)
  68. {
  69. //缺少关闭逻辑导致没有及时关闭动画,显示看起来比较卡顿
  70. this.BeginAnimation(UserControl.OpacityProperty, null);
  71. this.Visibility = Visibility.Collapsed;
  72. this.Opacity = 1;
  73. }
  74. private void Toast_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  75. {
  76. if((bool)e.NewValue)
  77. {
  78. DoubleAnimation doubleAnimation = new DoubleAnimation();
  79. doubleAnimation.Completed += DoubleAnimation_Completed;
  80. doubleAnimation.From = 1;
  81. doubleAnimation.To = 0;
  82. doubleAnimation.BeginTime = BeginTime;
  83. doubleAnimation.Duration = Duration;
  84. this.BeginAnimation(UserControl.OpacityProperty, doubleAnimation);
  85. }
  86. }
  87. }
  88. }