题
那么,作为标题建议:
我有一个故事板,我想它的动画开始播放而无需代码干预。 这样做的原因要求是我针对Silverlight Embedded和我懒得现在重新编译我的应用程序也是如此。而且,未来想起来,这将是更容易改变动画只能在未来。
确实XAML有一个属性,使故事板的运行,一旦XAML负载?
解决方案
您可以使用Loaded事件开始你的故事板
请参阅MSDN中的示例: 故事板(Silverlight的)
这MSDN拣的示例:
<Canvas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Rectangle
x:Name="MyAnimatedRectangle"
Width="100"
Height="100"
Fill="Blue">
<Rectangle.Triggers>
<!-- Animates the rectangle's opacity.
This is the important part, the EventTrigger which will start our animation -->
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>
在对象矩形具有的属性。在Triggers属性,我们定义了一个EventTrigger时会发生此事件将触发。我们选择Rectangle.Loaded事件,这意味着加载时,将火;)
我们添加一个BeginStoryboard属性来开始我们的故事板,并添加一个故事板。此动画将使用在Opacity属性一个DoubleAnimation是,这意味着在5秒的持续时间,不透明度将逐渐淡出到零,和背面(自动翻转属性)和它会做这永远(所述RepeatBehaviour属性)。
其他提示
<UserControl x:Class="SOSMVVM.AniM11"
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
mc:Ignorable='d'
d:DesignWidth='640'
d:DesignHeight='480'>
<StackPanel Margin="5">
<Rectangle Name="rect3" Fill="Blue" Margin="2" Width="20"
Height="20" HorizontalAlignment="Left" />
<Button Margin="2,20,0,0" HorizontalAlignment="Left"
Content="Start Animations" Width="100">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="rect3" Storyboard.TargetProperty="Width"
From="20" To="400" Duration="0:0:10" SpeedRatio="0.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
</UserControl>
不隶属于 StackOverflow