Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 1de0cb0

Browse files
committed
Remove XamlAnimatedGif
1 parent ed07c58 commit 1de0cb0

File tree

14 files changed

+167
-57
lines changed

14 files changed

+167
-57
lines changed

CookPopularControl/Communal/Data/Enum/SelectorItemType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ public enum SelectorItemType
2626
/// </summary>
2727
Icon,
2828
Image,
29+
Gif,
2930
}
3031
}

CookPopularControl/Controls/Button/ButtonAssist.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public class ButtonAssist
2222
{
2323
private static readonly Style DefaultProgressBarStyle = ResourceHelper.GetResource<Style>("LinearProgressBarBaseStyle");
2424

25+
26+
public static bool GetIsAutoStart(DependencyObject obj) => (bool)obj.GetValue(IsAutoStartProperty);
27+
public static void SetIsAutoStart(DependencyObject obj, bool value) => obj.SetValue(IsAutoStartProperty, ValueBoxes.BooleanBox(value));
28+
public static readonly DependencyProperty IsAutoStartProperty =
29+
DependencyProperty.RegisterAttached("IsAutoStart", typeof(bool), typeof(ButtonAssist), new PropertyMetadata(ValueBoxes.FalseBox));
30+
31+
2532
public static Uri GetGifSource(DependencyObject obj) => (Uri)obj.GetValue(GifSourceProperty);
2633
public static void SetGifSource(DependencyObject obj, Uri value) => obj.SetValue(GifSourceProperty, value);
2734
public static readonly DependencyProperty GifSourceProperty =

CookPopularControl/Controls/Picture/Gif.cs

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CookPopularCSharpToolkit.Windows.Interop;
1+
using CookPopularCSharpToolkit.Communal;
2+
using CookPopularCSharpToolkit.Windows.Interop;
23
using System;
34
using System.Collections.Generic;
45
using System.Drawing;
@@ -28,15 +29,47 @@
2829
namespace 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();

CookPopularControl/CookPopularControl.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
<ItemGroup>
8888
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
8989
<PackageReference Include="QRCoder" Version="1.4.1" />
90-
<PackageReference Include="XamlAnimatedGif" Version="2.0.2" />
9190
</ItemGroup>
9291

9392
<ItemGroup>

CookPopularControl/CookPopularControl.xml

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CookPopularControl/Themes/BaseStyle/ButtonBaseStyle.xaml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:common="clr-namespace:CookPopularControl.Communal"
4-
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
54
xmlns:pc="clr-namespace:CookPopularControl.Controls"
65
xmlns:pt="https://Chance.CookPopularCSharpToolkit/2021/xaml">
76

@@ -43,16 +42,20 @@
4342
SnapsToDevicePixels="True"
4443
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
4544
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
46-
<Image x:Name="PART_Image_Gif"
45+
<Image x:Name="PART_Image"
4746
DockPanel.Dock="{TemplateBinding common:FrameworkElementBaseAttached.IconDirection}"
4847
Width="{TemplateBinding pc:ButtonAssist.ImageWidth}"
4948
Height="{TemplateBinding pc:ButtonAssist.ImageHeight}"
5049
Source="{TemplateBinding pc:ButtonAssist.ImageSource}"
51-
Stretch="Uniform"
52-
gif:AnimationBehavior.AutoStart="{TemplateBinding gif:AnimationBehavior.AutoStart}"
53-
gif:AnimationBehavior.RepeatBehavior="{TemplateBinding gif:AnimationBehavior.RepeatBehavior}"
54-
gif:AnimationBehavior.SourceStream="{TemplateBinding pc:ButtonAssist.GifStream}"
55-
gif:AnimationBehavior.SourceUri="{TemplateBinding pc:ButtonAssist.GifSource}" />
50+
Stretch="Uniform" />
51+
<pc:Gif x:Name="PART_Gif"
52+
DockPanel.Dock="{TemplateBinding common:FrameworkElementBaseAttached.IconDirection}"
53+
Width="{TemplateBinding pc:ButtonAssist.ImageWidth}"
54+
Height="{TemplateBinding pc:ButtonAssist.ImageHeight}"
55+
Stretch="Uniform"
56+
GifSource="{TemplateBinding pc:ButtonAssist.GifSource}"
57+
GifStream="{TemplateBinding pc:ButtonAssist.GifStream}"
58+
IsAutoStart="{TemplateBinding pc:ButtonAssist.IsAutoStart}" />
5659
<Grid x:Name="ContentGrid">
5760
<ProgressBar x:Name="PART_Progress"
5861
Width="{TemplateBinding Width}"

CookPopularControl/Themes/BaseStyle/ComboBoxBaseStyle.xaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:common="clr-namespace:CookPopularControl.Communal"
4-
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
54
xmlns:pc="clr-namespace:CookPopularControl.Controls"
65
xmlns:pt="https://Chance.CookPopularCSharpToolkit/2021/xaml">
76

@@ -91,7 +90,7 @@
9190
</Setter.Value>
9291
</Setter>
9392
</Style>
94-
93+
9594
<!-- SimpleComboBox -->
9695
<Style x:Key="ComboBoxBaseStyle" TargetType="{x:Type ComboBox}">
9796
<Setter Property="Height" Value="{DynamicResource DefaultControlHeight}" />
@@ -333,8 +332,13 @@
333332
Height="{Binding ElementName=ItemGrid, Path=Height}"
334333
Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.ImageSource}"
335334
common:SelectorAttached.IsPreviewImage="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=(common:SelectorAttached.IsPreviewImage)}"
336-
gif:AnimationBehavior.SourceUri="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.GifSource}"
337335
Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=(common:SelectorAttached.SelectorItemType), Converter={common:SelectorItemTypeToVisibilityConverter}, ConverterParameter=Image}" />
336+
<pc:Gif x:Name="ItemGif"
337+
Width="{Binding ElementName=ItemGrid, Path=Width}"
338+
Height="{Binding ElementName=ItemGrid, Path=Height}"
339+
common:SelectorAttached.IsPreviewImage="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=(common:SelectorAttached.IsPreviewImage)}"
340+
GifSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.GifSource}"
341+
Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=(common:SelectorAttached.SelectorItemType), Converter={common:SelectorItemTypeToVisibilityConverter}, ConverterParameter=Gif}" />
338342
</Grid>
339343
<Popup x:Name="Popup_PreviewImage"
340344
AllowsTransparency="True" Placement="Right" PopupAnimation="Slide">
@@ -382,7 +386,7 @@
382386
</Setter.Value>
383387
</Setter>
384388
</Style>
385-
389+
386390
<!-- MultiComboBox -->
387391
<Style x:Key="MultiComboBoxBaseStyle" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource ComboBoxBaseStyle}">
388392
<Setter Property="Height" Value="{DynamicResource DefaultControlHeight}" />

CookPopularControl/Themes/BaseStyle/ListBoxItemBaseStyle.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:common="clr-namespace:CookPopularControl.Communal"
4-
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
54
xmlns:pc="clr-namespace:CookPopularControl.Controls"
65
xmlns:pt="https://Chance.CookPopularCSharpToolkit/2021/xaml">
76

@@ -65,8 +64,13 @@
6564
Height="{Binding ElementName=ItemGrid, Path=Height}"
6665
Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.ImageSource}"
6766
common:SelectorAttached.IsPreviewImage="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, Path=(common:SelectorAttached.IsPreviewImage)}"
68-
gif:AnimationBehavior.SourceUri="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.GifSource}"
6967
Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, Path=(common:SelectorAttached.SelectorItemType), Converter={common:SelectorItemTypeToVisibilityConverter}, ConverterParameter=Image}" />
68+
<pc:Gif x:Name="ItemGif"
69+
Width="{Binding ElementName=ItemGrid, Path=Width}"
70+
Height="{Binding ElementName=ItemGrid, Path=Height}"
71+
common:SelectorAttached.IsPreviewImage="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, Path=(common:SelectorAttached.IsPreviewImage)}"
72+
GifSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.GifSource}"
73+
Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, Path=(common:SelectorAttached.SelectorItemType), Converter={common:SelectorItemTypeToVisibilityConverter}, ConverterParameter=Gif}" />
7074
</Grid>
7175
<Popup x:Name="Popup_PreviewImage"
7276
AllowsTransparency="True" IsOpen="False" Placement="Right"

CookPopularControl/Themes/ButtonStyle.xaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:common="clr-namespace:CookPopularControl.Communal"
4-
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
54
xmlns:pc="clr-namespace:CookPopularControl.Controls">
65

76
<ResourceDictionary.MergedDictionaries>
@@ -24,16 +23,13 @@
2423
<Setter Property="pc:ButtonAssist.ImageSource" Value="{x:Null}" />
2524
<Setter Property="pc:ButtonAssist.ImageWidth" Value="0" />
2625
<Setter Property="pc:ButtonAssist.ImageHeight" Value="0" />
27-
<Setter Property="pc:ButtonAssist.GifStream" Value="{x:Null}" />
26+
<Setter Property="pc:ButtonAssist.IsAutoStart" Value="False" />
27+
<!--<Setter Property="pc:ButtonAssist.GifStream" Value="{x:Null}" />-->
2828
<Setter Property="pc:ButtonAssist.GifSource" Value="{x:Null}" />
2929
<Setter Property="pc:ButtonAssist.IsShowProgress" Value="False" />
3030
<Setter Property="pc:ButtonAssist.ProgressBarBrush" Value="{x:Null}" />
3131
<Setter Property="pc:ButtonAssist.ProgressValue" Value="0" />
3232
<Setter Property="pc:ButtonAssist.ProgressBarStyle" Value="{DynamicResource DefaultProgressBarStyle}" />
33-
<Setter Property="gif:AnimationBehavior.AutoStart" Value="True" />
34-
<Setter Property="gif:AnimationBehavior.RepeatBehavior" Value="0x" />
35-
<Setter Property="gif:AnimationBehavior.SourceStream" Value="{x:Null}" />
36-
<Setter Property="gif:AnimationBehavior.SourceUri" Value="{x:Null}" />
3733
</Style>
3834
<Style x:Key="ButtonOutlineStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultButtonStyle}">
3935
<Setter Property="Foreground" Value="{DynamicResource PrimaryForegroundBrush}" />

CookPopularControl/Themes/ToggleButtonStyle.xaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:common="clr-namespace:CookPopularControl.Communal"
4-
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
54
xmlns:pc="clr-namespace:CookPopularControl.Controls"
65
xmlns:pt="https://Chance.CookPopularCSharpToolkit/2021/xaml">
76

@@ -23,16 +22,13 @@
2322
<Setter Property="pc:ButtonAssist.ImageSource" Value="{x:Null}" />
2423
<Setter Property="pc:ButtonAssist.ImageWidth" Value="0" />
2524
<Setter Property="pc:ButtonAssist.ImageHeight" Value="0" />
26-
<Setter Property="pc:ButtonAssist.GifStream" Value="{x:Null}" />
25+
<Setter Property="pc:ButtonAssist.IsAutoStart" Value="False" />
26+
<!--<Setter Property="pc:ButtonAssist.GifStream" Value="{x:Null}" />-->
2727
<Setter Property="pc:ButtonAssist.GifSource" Value="{x:Null}" />
2828
<Setter Property="pc:ButtonAssist.IsShowProgress" Value="False" />
2929
<Setter Property="pc:ButtonAssist.ProgressBarBrush" Value="{x:Null}" />
3030
<Setter Property="pc:ButtonAssist.ProgressValue" Value="0" />
3131
<Setter Property="pc:ButtonAssist.ProgressBarStyle" Value="{DynamicResource DefaultProgressBarStyle}" />
32-
<Setter Property="gif:AnimationBehavior.AutoStart" Value="True" />
33-
<Setter Property="gif:AnimationBehavior.RepeatBehavior" Value="0x" />
34-
<Setter Property="gif:AnimationBehavior.SourceStream" Value="{x:Null}" />
35-
<Setter Property="gif:AnimationBehavior.SourceUri" Value="{x:Null}" />
3632
<Style.Triggers>
3733
<Trigger Property="IsChecked" Value="{x:Null}">
3834
<Setter Property="common:FrameworkElementBaseAttached.IconGeometry" Value="{x:Null}"/>

0 commit comments

Comments
 (0)