• 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

  • 相关阅读:
    QTP 参数化
    功能自动化测试流程
    Oracle客户端安装及配置
    描述性编程与对象库编程的对比
    Java用Scanner类获取用户输入
    Java入门的程序汇总
    Java入门学习知识点汇总
    Java最常用的变量定义汇总
    eclipse对Java程序的移植
    JavaScript关闭窗口的同时打开新页面的方法
  • 原文地址:https://www.cnblogs.com/8u7tgyjire7890/p/13281952.html
Copyright © 2020-2023  润新知