1- using CookPopularCSharpToolkit . Windows . Interop ;
1+ using CookPopularCSharpToolkit . Communal ;
2+ using CookPopularCSharpToolkit . Windows . Interop ;
23using System ;
34using System . Collections . Generic ;
45using System . Drawing ;
2829namespace CookPopularControl . Controls
2930{
3031 /// <summary>
31- /// GIF动画
32+ /// GIF动画控件
3233 /// </summary>
3334 /// <remarks>
3435 /// https://github.com/XamlAnimatedGif/XamlAnimatedGif
3536 /// </remarks>
3637 public class Gif : System . Windows . Controls . Image
3738 {
38- private Bitmap _gifBitmap ;
39- private BitmapSource _bitmapSource ;
39+ private Bitmap _gifBitmap ; //Gif图
40+ private BitmapSource _bitmapSource ; //Gif图的每一帧
41+ private bool _isStartGif ;
42+ private bool _isSetParameters ;
43+
44+
45+
46+ /// <summary>
47+ /// 是否自动启动
48+ /// </summary>
49+ public bool IsAutoStart
50+ {
51+ get => ( bool ) GetValue ( IsAutoStartProperty ) ;
52+ set => SetValue ( IsAutoStartProperty , ValueBoxes . BooleanBox ( value ) ) ;
53+ }
54+ /// <summary>
55+ /// 提供<see cref="IsAutoStart"/>的依赖属性
56+ /// </summary>
57+ public static readonly DependencyProperty IsAutoStartProperty =
58+ DependencyProperty . Register ( "IsAutoStart" , typeof ( bool ) , typeof ( Gif ) , new PropertyMetadata ( ValueBoxes . FalseBox , OnIsAutoStartPropertyChanged ) ) ;
59+
60+ private static void OnIsAutoStartPropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
61+ {
62+ if ( d is Gif gif )
63+ {
64+ var isStart = ( bool ) e . NewValue ;
65+
66+ if ( isStart && ( gif . GifSource != null || gif . GifStream != null ) )
67+ gif . StartAnimate ( ) ;
68+ else
69+ gif . StopAnimate ( ) ;
70+ }
71+ }
72+
4073
4174 /// <summary>
4275 /// Gif的路径
@@ -50,35 +83,79 @@ public Uri GifSource
5083 /// 表示<see cref="GifSource"/>的依赖属性
5184 /// </summary>
5285 public static readonly DependencyProperty GifSourceProperty =
53- DependencyProperty . Register ( "GifSource" , typeof ( Uri ) , typeof ( Gif ) , new UIPropertyMetadata ( default ( Uri ) , GifSourcePropertyChanged ) ) ;
54- protected static void GifSourcePropertyChanged ( DependencyObject obj , DependencyPropertyChangedEventArgs e )
86+ DependencyProperty . Register ( "GifSource" , typeof ( Uri ) , typeof ( Gif ) , new UIPropertyMetadata ( default ( Uri ) , OnGifSourcePropertyChanged ) ) ;
87+
88+ protected static void OnGifSourcePropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
5589 {
56- if ( obj is Gif gif )
90+ if ( d is Gif gif )
5791 {
5892 gif . GifSource = e . NewValue as Uri ;
5993 var stream = Application . GetResourceStream ( gif . GifSource ) . Stream ;
94+ gif . SetGifParameters ( gif , stream ) ;
95+
96+ if ( gif . IsAutoStart && ! gif . _isStartGif )
97+ gif . StartAnimate ( ) ;
98+
99+ gif . GifStream = stream ;
100+ }
101+ }
102+
103+
104+ /// <summary>
105+ /// Gif的流
106+ /// </summary>
107+ public Stream GifStream
108+ {
109+ get => ( Stream ) GetValue ( GifStreamProperty ) ;
110+ set => SetValue ( GifStreamProperty , value ) ;
111+ }
112+ /// <summary>
113+ /// 提供<see cref="GifStream"/>的依赖属性
114+ /// </summary>
115+ public static readonly DependencyProperty GifStreamProperty =
116+ DependencyProperty . Register ( "GifStream" , typeof ( Stream ) , typeof ( Gif ) , new PropertyMetadata ( default ( Stream ) , OnGifStreamPropertyChanged ) ) ;
117+
118+ private static void OnGifStreamPropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
119+ {
120+ if ( d is Gif gif )
121+ {
122+ gif . GifStream = e . NewValue as Stream ;
123+ gif . SetGifParameters ( gif , gif . GifStream ) ;
124+
125+ if ( gif . IsAutoStart && ! gif . _isStartGif )
126+ gif . StartAnimate ( ) ;
127+ }
128+ }
129+
130+ private void SetGifParameters ( Gif gif , Stream stream )
131+ {
132+ if ( ! gif . _isSetParameters )
133+ {
60134 gif . _gifBitmap = new Bitmap ( stream ) ;
61135 gif . _bitmapSource = gif . GetBitmapSource ( ) ;
62136 gif . Source = gif . _bitmapSource ;
63- gif . StartAnimate ( ) ;
64137 }
138+ _isSetParameters = true ;
65139 }
66140
141+
67142 public void StartAnimate ( )
68143 {
144+ _isStartGif = true ;
69145 ImageAnimator . Animate ( this . _gifBitmap , this . OnFrameChanged ) ;
70146 }
71147
72148 public void StopAnimate ( )
73149 {
150+ _isStartGif = false ;
74151 ImageAnimator . StopAnimate ( this . _gifBitmap , this . OnFrameChanged ) ;
75152 }
76153
77154 private void OnFrameChanged ( object sender , EventArgs e )
78155 {
79156 Dispatcher . BeginInvoke ( DispatcherPriority . Normal , new Action ( ( ) =>
80157 {
81- ImageAnimator . UpdateFrames ( ) ;
158+ ImageAnimator . UpdateFrames ( ) ;
82159 if ( this . _bitmapSource != null )
83160 {
84161 this . _bitmapSource . Freeze ( ) ;
0 commit comments