我有以下设置。
<Window.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingPath
Duration="0:0:2"
Source="X"
Completed="Timeline_OnCompleted"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M42.473003,3.8059855 L281.428,3.8059855"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<DoubleAnimationUsingPath Duration="0:0:2" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)">
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M42.473003,3.8059855 L281.428,3.8059855"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
</Storyboard>
<Storyboard x:Key="Storyboard2">
<DoubleAnimationUsingPath Duration="0:0:2" Source="X" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M286.789,2.116 L420.289,-184.884 L306.789,-240.384"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<DoubleAnimationUsingPath Duration="0:0:2" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)">
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M286.789,2.116 L420.289,-184.884 L306.789,-240.384"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
</Storyboard>
</Window.Resources>
<Grid>
<Canvas x:Name="Animation_Path" Canvas.Left="3.409" Canvas.Top="53.412" Margin="155.184,649.19,0,0" RenderTransformOrigin="0.5,0.5"/>
<Button Content="Start" HorizontalAlignment="Left" Margin="588,252,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_5"/>
</Grid>
它背后的代码。
private void Button_Click_5(object sender, RoutedEventArgs e)
{
ObjectToMove move = new ObjectToMove();
Animation_Path.Children.Add(move);
var sb1 = FindResource("Storyboard1") as Storyboard;
sb1.Begin(move);
}
我得到了这个工作。但是,在第一个完成后,如何才能将新创建的对象传递给第二个storyboard2
。如果可能,我更喜欢在代码中这样做。
答案 0 :(得分:1)
根据我的理解,您可以使用Completed
事件。让我们试试这个,有多个故事板序列。
PS。有关将事件处理程序转换为等待格式的任何注释都是受欢迎的。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
private void Button_Click_5(object sender, RoutedEventArgs e)
{
var move1 = new ObjectToMove();
Animation_Path.Children.Add(move1);
var storyBoardsToRun = new[] {"Storyboard1", "Storyboard2"};
storyBoardsToRun
.Select(sbName => FindResource(sbName) as Storyboard)
.ToList()
.ForEach(async sb => await sb.BeginAsync(move1));
}
public static class StoryBoardExtensions
{
public static Task BeginAsync(this Storyboard sb, FrameworkContentElement element)
{
var source = new TaskCompletionSource<object>();
sb.Completed += delegate
{
source.SetResult(null);
};
sb.Begin(element);
return source.Task;
}
}