从C#代码启动Storyboard [英] Start Storyboard from C# code
问题描述
我正在尝试从C#调用以xaml代码声明的情节提要:
I am trying to call a storyboard declared in xaml code from C#:
<Storyboard x:Key="BotRotation" Duration="00:00:4" RepeatBehavior="Forever" >
<DoubleAnimation BeginTime="0:0:0"
Storyboard.TargetName="imageRotateTransformm"
Storyboard.TargetProperty="ScaleX"
From="1" To="-1"
Duration="0:0:2"
/>
<DoubleAnimation
BeginTime="0:0:2"
Storyboard.TargetName="imageRotateTransformm"
Storyboard.TargetProperty="ScaleX"
From="-1" To="1"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
此故事板应修改图像的ScaleX属性.图片声明:
This storyboard should modify the ScaleX property of image. Image Declaration:
<Image Name="uiRobotIcon" Height="64" Width="64" Source="/YoutubeTelegramAudio;component/imgs/ic_robot.png" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="1" x:Name="imageRotateTransformm" />
<SkewTransform AngleY="0" AngleX="0" />
<RotateTransform Angle="0" />
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
然后,当我单击另一个按钮时,我想从C#启动此动画.谢谢.
Then, i want to start this animation from C# when i click another button. Thanks.
推荐答案
那很容易.只需找到 Resource
,将其转换为 Storyboard
,然后调用其
That's very easy. Just find the Resource
, cast it to a Storyboard
and then call its Begin()
method:
Storyboard sb = (<YourNamespace>.Properties.Resources["BotRotation"] as Storyboard);
sb.Begin();
将上面的代码放在某个事件处理程序中,它应该可以正常工作.
Put the above code in some event handler and it should work fine.
在应用程序中声明情节提要的位置也很重要.如果它在 App.xaml
中,则别无其他担心,并且可能无法访问.但是我认为 Window.Resources
标记也可以包含可访问的资源.我现在无法测试:)
It also matters where in the application you declare the Storyboard. If it is in the App.xaml
, then no worries but anywhere else, and it might be unaccessible. But I think the Window.Resources
tag can also contain accessible resources. I can't test it right now :)
这篇关于从C#代码启动Storyboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!