zoukankan      html  css  js  c++  java
  • wpf 中用 C# 代码创建 PropertyPath ,以对间接目标进行 Storyboard 动画.

    如图,一个 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

  • 相关阅读:
    call和apply的区别
    淘宝镜像(cnpm)的安装和使用
    文件包含漏洞
    vue简单的日历
    微信小程序(mpvue)—解决视频播放bug的一种方式
    vue 异步组件
    vuex的学习笔记
    vue2.0 添加监听滚动事件
    jquery tmpl生成导航
    vue 控制视图
  • 原文地址:https://www.cnblogs.com/8u7tgyjire7890/p/13281952.html
Copyright © 2011-2022 走看看