简体   繁体   English

Storyboard DoubleAnimation 以编程方式

[英]Storyboard DoubleAnimation programmatically

i try to do this xaml code programmatically我尝试以编程方式执行此 xaml 代码

<Grid.Triggers>
    <EventTrigger RoutedEvent="UIElement.MouseEnter">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="tranny" Storyboard.TargetProperty="ScaleX" To="1.2" Duration="0:0:1">
                    <DoubleAnimation.EasingFunction>
                         <ElasticEase Oscillations="1" Springiness="8"/>
                     </DoubleAnimation.EasingFunction>
                 </DoubleAnimation>
                 <DoubleAnimation Storyboard.TargetName="tranny" Storyboard.TargetProperty="ScaleY" To="1.2" Duration="0:0:1">
                     <DoubleAnimation.EasingFunction>
                         <ElasticEase Oscillations="1" Springiness="8" />
                     </DoubleAnimation.EasingFunction>
                 </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Grid.Triggers>

it do it but it didn't work with i don't know why but there is no errors if there is any onwe can write this xaml in C# programmatically answer me它做到了,但它不起作用我不知道为什么但是如果有任何错误没有错误我们可以在 C# 中写这个 xaml 以编程方式回答我

please if you have answer can help me add it if no please don't give -1 that is programmatically code that i wrote请如果你有答案可以帮助我添加它如果没有请不要给-1这是我编写的编程代码

var secondryGrid = new Grid();
var elasticEase = new ElasticEase();
elasticEase.Oscillations = 1;
elasticEase.Springiness = 8;
var timeSpan = new TimeSpan(0, 0, 0, 100);
var duration = new Duration(timeSpan);
var doubleAnimation = new DoubleAnimation();
doubleAnimation.To = 1.2;
doubleAnimation.Duration = duration;
doubleAnimation.EasingFunction = elasticEase;
var path = new PropertyPath("ScaleX");
var storyBoard = new Storyboard();
Storyboard.SetTargetProperty(doubleAnimation, path);
storyBoard.Children.Add(doubleAnimation);
var beginStoryBoard = new BeginStoryboard();
beginStoryBoard.Storyboard = storyBoard;
elasticEase = new ElasticEase();
elasticEase.Oscillations = 1;
elasticEase.Springiness = 8;
timeSpan = new TimeSpan(0, 0, 0, 100);
duration = new Duration(timeSpan);
doubleAnimation = new DoubleAnimation();
doubleAnimation.To = 1.2;
doubleAnimation.Duration = duration;
doubleAnimation.EasingFunction = elasticEase;
path = new PropertyPath("ScaleY");
Storyboard.SetTargetProperty(doubleAnimation, path);
storyBoard.Children.Add(doubleAnimation);
var gridEventTrigger = new EventTrigger();
gridEventTrigger.RoutedEvent = MouseEnterEvent;
gridEventTrigger.Actions.Add(beginStoryBoard);
secondryGrid.Triggers.Add(gridEventTrigger);

sorry for my bad language对不起我的语言不好

In code, this is done somewhat easier.在代码中,这更容易完成。 You don't need to instantiate BeginStoryboard and Storyboard;您不需要实例化 BeginStoryboard 和 Storyboard; these classes are designed to make it easier to use animations in XAML.这些类旨在使在 XAML 中使用动画更容易。 In code, you can set the animation directly to the desired property.在代码中,您可以将 animation 直接设置为所需的属性。 This requires significantly less code.这需要更少的代码。

Example:例子:

        {
            Grid grid = new Grid();
            grid.MouseEnter += OnGridMouseEnter;
        }

        // An animation instance is created once and can then be reused.
        private static readonly DoubleAnimation xyAnimation = new DoubleAnimation(1.2, TimeSpan.FromSeconds(1))
        {
            EasingFunction = new ElasticEase()
            {
                Oscillations = 1,
                Springiness = 8
            }
        };

        private void OnGridMouseEnter(object sender, MouseEventArgs e)
        {
            tranny.BeginAnimation(ScaleTransform.ScaleXProperty, xyAnimation);
            tranny.BeginAnimation(ScaleTransform.ScaleYProperty, xyAnimation);
        }

PS It is not clear from your code how you create the tranny transformation. PS从您的代码中不清楚如何创建tranny转换。
My example is created on the assumption that there is a tranny field that contains the transformation that needs to be animated.我的示例是在假设存在一个包含需要动画的转换的tranny字段的情况下创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DoubleAnimation / Storyboard不会停止 - DoubleAnimation/Storyboard doesn't stop WPF故事板:DoubleAnimation可在不透明度上起作用,但不能TranslateTransform吗? - WPF Storyboard: DoubleAnimation Works On Opacity But Not TranslateTransform? 带Storyboard.TargetProperty =“ Height”的图片的DoubleAnimation - DoubleAnimation for image with Storyboard.TargetProperty=“Height” Storyboard DoubleAnimation不适用于StackPanel高度属性 - Storyboard DoubleAnimation Does not work with StackPanel Height Property DoubleAnimation / Storyboard完成两次并重置动画值 - DoubleAnimation / Storyboard completes twice and resets animation value WPF DoubleAnimation 旋转方向无 storyboard - WPF DoubleAnimation rotation direction without storyboard 如何在更长的Storyboard内部的DoubleAnimation结束时调用回调? - How to invoke callback at end of DoubleAnimation inside of a longer Storyboard? 如何使用Windows 8.1中的Storyboard DoubleAnimation为ProgressBar值属性设置动画 - How to animate a ProgressBar Value Property using Storyboard DoubleAnimation in Windows 8.1 停止以编程方式实例化的情节提要 - Stopping a programmatically instantiated storyboard 在WPF中,我看不到C#故事板和DoubleAnimation有什么问题 - In WPF I can't see whats wrong with my C# Storyboard and DoubleAnimation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM