如图,一个 Rectangle 一个 Button ,点击按钮时要通过动画完成对 Rectangle填充色的渐变动画.

Xaml:

 1 <Window
2 x:Class="WpfApp1.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7 Title="MainWindow"
8 Width="400"
9 Height="250"
10 mc:Ignorable="d">
11 <Grid>
12 <Grid.RowDefinitions>
13 <RowDefinition />
14 <RowDefinition />
15 </Grid.RowDefinitions>
16
17 <!-- 注意这里通过 x:Name 为 Rectangle 对象注册了名称 -->
18 <!-- Rectangle 继承于 Shape. -->
19 <!-- Fill 是 Shape 的依赖属性,类型是 Brush. 这里为其指定了一个 SolidColorBrush 类型的笔刷,其 Color 属性为 Aqua -->
20 <!-- 接下来要在后台的C#代码中对这个 SolidColorBrush 的 Color 属性应用动画 -->
21 <Rectangle
22 x:Name="TheRectangle"
23 Width="150"
24 Height="50"
25 Fill="Aqua" />
26
27 <Button
28 Grid.Row="1"
29 Width="150"
30 Height="50"
31 Click="ButtonBase_OnClick">
32 开始动画
33 </Button>
34 </Grid>
35 </Window>

C#

 1 using System;
2 using System.Windows;
3 using System.Windows.Media;
4 using System.Windows.Media.Animation;
5 using System.Windows.Shapes;
6
7 namespace WpfApp1
8 {
9 /// <summary>
10 /// MainWindow.xaml 的交互逻辑
11 /// </summary>
12 public partial class MainWindow
13 {
14 public MainWindow()
15 {
16 InitializeComponent();
17 }
18
19 private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
20 {
21 //创建颜色动画对象.
22 var colorAnimation = new ColorAnimation(
23 Colors.Aqua, //颜色起始值
24 Colors.BlueViolet, //颜色中值值
25 new Duration(new TimeSpan(0, 0, 2)) //动画持续时间,2秒
26 );
27
28 //创建属性链.
29 //动画的目标属性是一个 Shape.Fill 属性的 Color 子属性.
30 var propertyChain = new[]
31 {
32 Shape.FillProperty,
33 SolidColorBrush.ColorProperty
34 };
35
36 //通过属性链创建 PropertyPath 对象.
37 var propertyPath = new PropertyPath("(0).(1)", propertyChain);
38
39 //通过 PropertyPath 对象指定动画的目标属性.
40 Storyboard.SetTargetProperty(colorAnimation, propertyPath);
41
42 //指定动画的目标对象
43 Storyboard.SetTargetName(colorAnimation, "TheRectangle");
44
45 //创建故事版,将动画包含其中,并启动动画
46 var storyboard = new Storyboard();
47 storyboard.Children.Add(colorAnimation);
48 storyboard.Begin(this);
49 }
50 }
51 }

知识来源:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/graphics-multimedia/storyboards-overview#indirect-targeting

最新文章

  1. CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
  2. 2016 Multi-University Training Contest 8
  3. 20145207 《Java程序设计》第5周学习总结
  4. Hadoop :map+shuffle+reduce和YARN笔记分享
  5. linux安装rzsz
  6. JS匿名执行函数
  7. css 单位 px em rem
  8. 初识JavaScript,感觉整个人都不好了。。。
  9. html:打开新的页面
  10. iptables配置管理
  11. spring boot1.0 集成quartz 动态配置定时任务
  12. angular学习笔记(1)- 四大核心特性
  13. 11. English vocabulary 英语词汇量
  14. Mac 安装微软雅黑字体
  15. onclick或者其他事件在部分移动端无效的问题
  16. MySQL 设置密码有效时间
  17. UVa 12174 Shuffle(滑动窗口)
  18. Java中的时间日期处理
  19. Netty源码分析第5章(ByteBuf)----&gt;第9节: ByteBuf回收
  20. 基础知识:Promise(整理)

热门文章

  1. STL——容器(Map &amp; multimap)的简述与构造
  2. 最新快手抖音短视频源码web+APP架设教程+完整数据
  3. sql注入之union注入
  4. 第七周jieba分词
  5. 七牛云上传视频(后端获取tolen)
  6. Durid的特点
  7. Web服务器-HTTP相关-快速整一个服务器响应浏览器(3.2.1)
  8. Yii 文件上传类的使用
  9. 如何使用阿里云云解析API实现动态域名解析,搭建私有服务器
  10. Office__自动保存和恢复