gpt4 book ai didi

c# - 将新创建的对象传递给多个 Storyboard

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:48 26 4
gpt4 key购买 nike

我有以下设置。

    <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 ..?如果可能的话,我更喜欢在代码中这样做。

最佳答案

据我了解,您可以为此使用 Completed 事件。让我们尝试一下,使用多个 Storyboard序列。

附言。欢迎任何关于将事件处理程序转换为等待格式的评论。

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

关于c# - 将新创建的对象传递给多个 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com