首页 > 其他分享 >WPF Animation 动画变化值的监控

WPF Animation 动画变化值的监控

时间:2024-08-16 15:41:40浏览次数:5  
标签:动画 System private da Animation using WPF MainWindow

WPF 动画

XXXAnimation 即关键类继承制AnimationBase 的动画类

  • 线性插值动画主要属性

    • From
    • To
    • Duration
    • Accelerator
    • Deccelerator
    • FillBehavior
      等这些常用属性里不做介绍,主要介绍一下几个故事板属性,以备忘记.
名称 说明
Completed 动画到达终点时触发,该事件中可以用于删除之前的动画,比如有些时候我们需要连续增加动画时,为了动画衔接可以在该事件中处理
CurrentGlobalSpeedInvalidated 速度发生了变化,或者动画被暂停、重新开始、停止或移到某个新的位置。当动画始终翻转时,以及当动画加速和减速时,也会引发改事件。用于监控速度变化
CurrentStateInvalidated 动画已经开始或结束
CurrentTimeInvalidate 这个属性要着重介绍一下,一看这里有个Time,那肯定是与始终有关系的,没错。该事件时动画始终已经向前移动了一个步长(By),正在更改动画。当动画开始、停止或结束时也会引发该事件。可以通过该事件监控动画变化的数值,这样我们可以通过回调向外通知变化值
RemoveRequest 动画正在被移除。使用动画的属性随后回返回位原来的值。动画移除方法是添加一个空的动画,如obj.BeginAnimation(xxxProperty,null)

下面通过一个小的示例介绍一下动画变化值的监控

  • XMAL
<Window x:Class="WpfApp10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" Width="30">
            <Rectangle VerticalAlignment="Bottom"   x:Name="r1" Height="0" Fill="Green" />
        </Border>
        <StackPanel Orientation="Vertical" Grid.Column="1">
            <TextBlock x:Name="txtVal" Width="120" Height="30" Text="0" FontSize="22" />
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="通过回调获取的值:" VerticalAlignment="Center"/>
                <TextBlock x:Name="txtCallValue" Foreground="Red" FontSize="22" Margin="10" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>
  • CS
using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WpfApp10
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Action<string> ReportValue;
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            ReportValue = (str) =>{
                this.Dispatcher.Invoke((Action)delegate{
                    txtCallValue.Text = str;
                });
            };
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.From = r1.Height;
            da.To = 300;
            da.Duration = new Duration(TimeSpan.FromMilliseconds(8000));
            da.CurrentTimeInvalidated += Da_CurrentTimeInvalidated;
            r1.BeginAnimation(Rectangle.HeightProperty, da);
            

        }

        private string s;
        private void Da_CurrentTimeInvalidated(object sender, EventArgs e)
        {
            Clock clock = (Clock)sender;
            this.Dispatcher.Invoke((Action)delegate
            {
                 s= string.Format("{0:N0}",clock.CurrentProgress * 300);
                 if (ReportValue != null) {
                    ReportValue(s);
                 }
                txtVal.Text = s;

            }  
            );
        }

      
    }
}

  • 效果
    image

标签:动画,System,private,da,Animation,using,WPF,MainWindow
From: https://www.cnblogs.com/sundh1981/p/18362959

相关文章

  • html5+CSS3 Canvas动画分享
    1.赛朋博客赛车动画 源码分享:<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <title>赛车</title>  <style>    *{      margin:0;      padding:0;      bo......
  • WPF 触发器
    一、样式触发器样式触发器可以在指定的控件属性满足某种条件后进行一些样式的变换,当触发条件不满足时恢复原样。样式触发器的简单使用<Window.Resources><Stylex:Key="checkBoxStyle"TargetType="CheckBox"><Style.Triggers><TriggerProperty="......
  • WPF 窗体关闭的方式
    1.Close();关闭当前窗口在WPF应用程序的关闭是有ShutdownMode属性设置,具有3中枚举类型的值:1)OnLastWindowClose(默认值)---应用程序最后一个窗体关闭时关闭应用程序2)OnMainWindowClose---应用程序主窗体关闭时关闭应用程序3)OnxplicitShutdown---显示调用关闭这......
  • WPF KeyDown MVVM Via Behavior
    <behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMethodActionMethodName="Window_KeyDown"TargetObject="{Binding}"/></behavior:EventTrigger>......
  • WPF Customize control
    //xaml<UserControlx:Class="WpfApp246.EllipseTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc=&q......
  • css水平移动动画
    <template><divid="app"><divclass="container"><div:class="{'moving':isMoving}"class="box"><button@click="moveBox">Move</button>......
  • Manim的一个用于数学动画的 Python 库中渲染代码的功能。
       Code 函数是Manim(一个强大的数学动画库)中的一个重要工具,旨在将代码片段以视觉化的方式呈现。在教育和演示场合中,向观众展示算法或代码逻辑时,清晰的视觉效果是必不可少的。通过 Code 函数,用户可以轻松地将特定编程语言的代码导入,并且自定义其外观,包括字体、颜色、背景......
  • 计算机图形学 | 动画模拟
    动画模拟布料模拟质点弹簧系统:红色部分很弱地阻挡对折SteepconnectionFEM:有限元方法粒子系统粒子系统本质上就是在定义个体和群体的关系。动画帧率VR游戏要不晕需要达到90fpsForwardKinematicsInverseKinematics只告诉末端p点,中间随便怎么连。解不一......
  • 【Material-UI】Floating Action Button (FAB) 详解:动画效果 (Animation)
    文章目录一、FAB按钮的动画概述1.默认动画效果2.多屏幕横向切换时的动画二、FAB动画效果的实现1.代码示例:跨标签页的FAB动画2.代码解析3.多个FAB的切换三、动画效果的最佳实践四、总结在现代网页设计中,动画不仅提升了用户界面的动态感,还增强了用户的交......
  • Anrdoir 13 关于设置静态IP后,突然断电,在上电开机卡动画
    bug描述:设置静态IP成功后,机器突然断电,然后在上电开机,发现机器一直卡在开机动画,无法成功进入桌面第一时间抓取日志分析,Log如下:08-1311:26:42.45528032803IEthernetServiceImpl:StartingEthernetservice08-1311:26:42.45728032924DConnectivityService:......