- 1 public class FluentSolidColorBrush : XamlCompositionBrushBase
- 2 {
- 3 Compositor Compositor => Window.Current.Compositor;
- 4 ColorKeyFrameAnimation ColorAnimation;
- 5 bool IsConnected;
- 6
- 7 //被设置到控件属性时触发,例RootGrid.Background=new FluentSolidColorBrush();
- 8 protected override void OnConnected()
- 9 {
- 10 if (CompositionBrush == null)
- 11 {
- 12 IsConnected = true;
- 13
- 14 ColorAnimation = Compositor.CreateColorKeyFrameAnimation();
- 15
- 16 //进度为0的关键帧,表达式为起始颜色。
- 17 ColorAnimation.InsertExpressionKeyFrame(0f, "this.StartingValue");
- 18
- 19 //进度为0的关键帧,表达式为参数名为Color的参数。
- 20 ColorAnimation.InsertExpressionKeyFrame(1f, "Color");
- 21
- 22 //创建颜色笔刷
- 23 CompositionBrush = Compositor.CreateColorBrush(Color);
- 24 }
- 25 }
- 26
- 27 //从属性中移除时触发,例RootGrid.Background=null;
- 28 protected override void OnDisconnected()
- 29 {
- 30 if (CompositionBrush != null)
- 31 {
- 32 IsConnected = false;
- 33
- 34 ColorAnimation.Dispose();
- 35 ColorAnimation = null;
- 36 CompositionBrush.Dispose();
- 37 CompositionBrush = null;
- 38
- 39 //清除已注册的事件。
- 40 ColorChanged = null;
- 41 }
- 42 }
- 43
- 44 public TimeSpan Duration
- 45 {
- 46 get { return (TimeSpan)GetValue(DurationProperty); }
- 47 set { SetValue(DurationProperty, value); }
- 48 }
- 49
- 50 public static readonly DependencyProperty DurationProperty =
- 51 DependencyProperty.Register("Duration", typeof(TimeSpan), typeof(FluentSolidColorBrush), new PropertyMetadata(TimeSpan.FromSeconds(0.4d), (s, a) =>
- 52 {
- 53 if (a.NewValue != a.OldValue)
- 54 {
- 55 if (s is FluentSolidColorBrush sender)
- 56 {
- 57 if (sender.ColorAnimation != null)
- 58 {
- 59 sender.ColorAnimation.Duration = (TimeSpan)a.NewValue;
- 60 }
- 61 }
- 62 }
- 63 }));
- 64
- 65 public Color Color
- 66 {
- 67 get { return (Color)GetValue(ColorProperty); }
- 68 set { SetValue(ColorProperty, value); }
- 69 }
- 70
- 71 public static readonly DependencyProperty ColorProperty =
- 72 DependencyProperty.Register("Color", typeof(Color), typeof(FluentSolidColorBrush), new PropertyMetadata(default(Color), (s, a) =>
- 73 {
- 74 if (a.NewValue != a.OldValue)
- 75 {
- 76 if (s is FluentSolidColorBrush sender)
- 77 {
- 78 if (sender.IsConnected)
- 79 {
- 80 //给ColorAnimation,进度为1的帧的参数Color赋值
- 81 sender.ColorAnimation.SetColorParameter("Color", (Color)a.NewValue);
- 82
- 83 //创建一个动画批,CompositionAnimation使用批控制动画完成。
- 84 var batch = sender.Compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
- 85
- 86 //批内所有动画完成事件,完成时如果画刷没有Disconnected,则触发ColorChanged
- 87 batch.Completed += (s1, a1) =>
- 88 {
- 89 if (sender.IsConnected)
- 90 {
- 91 sender.OnColorChanged((Color)a.OldValue, (Color)a.NewValue);
- 92 }
- 93 };
- 94 sender.CompositionBrush.StartAnimation("Color", sender.ColorAnimation);
- 95 batch.End();
- 96 }
- 97 }
- 98 }
- 99 }));
- 100
- 101 public event ColorChangedEventHandler ColorChanged;
- 102 private void OnColorChanged(Color oldColor, Color newColor)
- 103 {
- 104 ColorChanged?.Invoke(this, new ColorChangedEventArgs()
- 105 {
- 106 OldColor = oldColor,
- 107 NewColor = newColor
- 108 });
- 109 }
- 110 }
- 111
- 112 public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs args);
- 113 public class ColorChangedEventArgs : EventArgs
- 114 {
- 115 public Color OldColor { get; internal set; }
- 116 public Color NewColor { get; internal set; }
- 117 }