一、实现
使用StringAnimationUsingKeyFrames字符串动画关键帧来实现
示例代码:
private StringAnimationUsingKeyFrames myAnimation;
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModels.SendViewModel();
myAnimation = new StringAnimationUsingKeyFrames();
myAnimation.FillBehavior = FillBehavior.Stop;
for (int i = 6; i >= 0; i--)
{
myAnimation.KeyFrames.Add(
new DiscreteStringKeyFrame()
{
Value = $"{i}s后可再次发送",
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6 - i))
});
}
}
private void btn_Click(object sender, RoutedEventArgs e)
{
probar.Value = 0;//同时模拟进度条
btn.IsEnabled = false;
btn.Foreground = Brushes.Black;
myAnimation.Completed += (s, arg) =>
{
btn.IsEnabled = true;
};
btn.BeginAnimation(Button.ContentProperty, myAnimation);
}
效果图片:
环形进度条制作点击:WPF动画制作_扩展_进度条制作
二、添加加载进度条
示例代码:
GeometryCombineMode="Intersect":取交集
DoubleAnimation mydbAnimation = new DoubleAnimation();
mydbAnimation.From = -50;
mydbAnimation.To = 400;
mydbAnimation.Duration = new Duration(TimeSpan.FromSeconds(6));
tranForm.BeginAnimation(TranslateTransform.XProperty, mydbAnimation);
<Rectangle Width="400"
Height="20"
Margin="190,349,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"
Fill="LightGray" />
<Path Name="path"
Width="400"
Height="20" VerticalAlignment="Center" HorizontalAlignment="Center"
Margin="190,349,0,0"
Fill="DeepPink">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Intersect">
<CombinedGeometry.Geometry1>
<RectangleGeometry Rect="0,0,400,20" />
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<RectangleGeometry Rect="0,0,50,20">
<RectangleGeometry.Transform>
<TranslateTransform x:Name="tranForm" X="-6" />
</RectangleGeometry.Transform>
</RectangleGeometry>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
效果图: