多个元素的相同故事板动画

时间:2022-11-27 14:27:42

I recently started studying C# wpf. I want to know can we use same storyboard for multiple targets. I know this can be achieved with XAML but i don't know how to achieve it through c# code. I am doing something like when storyboard begins the animation then for 0-2sec one element completes animation and then from 2-4 sec duration another element does its animation. In this way the total duration of storyboard is 4 seconds. Below is the XAML code i found on the web, I want to do something like this in c# code

我最近开始学习C#wpf。我想知道我们可以为多个目标使用相同的故事板。我知道这可以通过XAML实现,但我不知道如何通过c#代码实现它。我做的事情就像故事板开始动画然后0-2秒一个元素完成动画然后从2-4秒持续时间另一个元素做动画。通过这种方式,故事板的总持续时间为4秒。下面是我在网上找到的XAML代码,我想在c#代码中做这样的事情

<Storyboard x:Name=”sbFlip“> 
    <DoubleAnimationUsingKeyFrames BeginTime=”00:00:00” Storyboard.TargetName=”front”  Storyboard.TargetProperty=”(UIElement.RenderTransform).(ScaleTransform.ScaleX)“> 
        <SplineDoubleKeyFrame KeyTime=”00:00:00.2” Value=”0“/> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames BeginTime=”00:00:00.2” Storyboard.TargetName=”back” Storyboard.TargetProperty=”(UIElement.RenderTransform).(ScaleTransform.ScaleX)“> 
        <SplineDoubleKeyFrame KeyTime=”00:00:00.4” Value=”1“/> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

1 个解决方案

#1


0  

Let These are the three objects in the View:

设这些是视图中的三个对象:

  <Rectangle x:Name="rctMovingObject1" Fill="LimeGreen" Width="50" Height="50"/>
  <Rectangle x:Name="rctMovingObject2" Fill="Red" Width="50" Height="50" Canvas.Top="136"/>
  <Rectangle x:Name="rctMovingObject3" Fill="Purple" Width="50" Height="50" Canvas.Top="69"/>

You can define an animation from back-end and assign to all of them as follows:

您可以从后端定义动画并分配给所有动画,如下所示:

var anim = new DoubleAnimation
  {
     From = 1920,
     To = 1,
     Duration =new Duration(new TimeSpan(0,0,2))// a duration of 2 seconds 
  };
rctMovingObject1.BeginAnimation(Canvas.LeftProperty, anim);
rctMovingObject2.BeginAnimation(Canvas.LeftProperty, anim);
rctMovingObject3.BeginAnimation(Canvas.LeftProperty, anim);

Read More...

阅读更多...

#1


0  

Let These are the three objects in the View:

设这些是视图中的三个对象:

  <Rectangle x:Name="rctMovingObject1" Fill="LimeGreen" Width="50" Height="50"/>
  <Rectangle x:Name="rctMovingObject2" Fill="Red" Width="50" Height="50" Canvas.Top="136"/>
  <Rectangle x:Name="rctMovingObject3" Fill="Purple" Width="50" Height="50" Canvas.Top="69"/>

You can define an animation from back-end and assign to all of them as follows:

您可以从后端定义动画并分配给所有动画,如下所示:

var anim = new DoubleAnimation
  {
     From = 1920,
     To = 1,
     Duration =new Duration(new TimeSpan(0,0,2))// a duration of 2 seconds 
  };
rctMovingObject1.BeginAnimation(Canvas.LeftProperty, anim);
rctMovingObject2.BeginAnimation(Canvas.LeftProperty, anim);
rctMovingObject3.BeginAnimation(Canvas.LeftProperty, anim);

Read More...

阅读更多...