故事板 DoubleAnimation 以编程方式

时间:2021-06-13 04:06:59

标签: c# wpf wpf-controls

我尝试以编程方式执行此 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>

它做到了,但它不起作用,我不知道为什么,但如果有任何错误,我们可以在 C# 中以编程方式编写此 xaml 回答我

如果您有答案可以帮我添加它,如果没有请不要给出-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);

抱歉我的语言不好

1 个答案:

答案 0 :(得分:0)

在代码中,这更容易一些。 您不需要实例化 BeginStoryboard 和 Storyboard;这些类旨在简化在 XAML 中使用动画的过程。 在代码中,您可以直接将动画设置为所需的属性。 这需要的代码要少得多。

示例:

        {
            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);
        }

P.S. 从您的代码中不清楚您是如何创建 tranny 转换的。
我的示例是在假设有一个 tranny 字段包含需要动画的转换的情况下创建的。