如何在代码后面设置变换动画?

问题描述 投票:0回答:1

我有一个按钮。出于某种原因,我想在后面的代码中应用转换。并且转换工作正常。但是当我尝试在变换上添加动画时,动画不会生效。我的代码如下。

   <Grid x:Name="grid">
       <Button Height="40" Width="120" Content="Button"
               x:Name="button" Click="button_Click">
    
       </Button>
   </Grid>



并且在单击按钮时的代码隐藏中是:

    private void button_Click(object sender, RoutedEventArgs e)
    {

        var animation = new DoubleAnimation(0, 20, new Duration(TimeSpan.FromSeconds(1)));
        var story = new Storyboard();
        story.Children.Add(animation);
        Storyboard.SetTarget(animation, button);
        Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));
     
        story.Begin();
    
    }
        private void button_Click(object sender, RoutedEventArgs e)
        {

            var animation = new DoubleAnimation(0, 20, new Duration(TimeSpan.FromSeconds(1)));
            var story = new Storyboard();
            story.Children.Add(animation);
            Storyboard.SetTarget(animation, button);
            Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));
         
            story.Begin();
        
        }
c# wpf animation storyboard transform
1个回答
0
投票

问题可能在于您调用

SetTarget
SetTargetProperty
的位置,它们似乎是
StoryBoard
类的静态方法。尝试使用新初始化的对象来代替。改变这个

Storyboard.SetTarget(animation, button);
Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));

对此

story.SetTarget(animation, button);
story.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));

这应该改变初始化对象的状态。

© www.soinside.com 2019 - 2024. All rights reserved.