好吧,正如标题所示:
我有一个故事板,我希望它的动画能够在没有代码干预的情况下启动。 这个要求的原因是我的目标是Silverlight Embedded,我现在也懒得重新编译我的应用程序。并且,考虑到它,将来更容易更改动画。
XAML是否有一个属性可以在xaml加载后立即运行故事板?
答案 0 :(得分:15)
您可以使用Loaded事件来启动故事板
有关示例,请参阅MSDN: Storyboard (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>
对象Rectangle具有属性。在Triggers属性中,我们定义了一个EventTrigger,它将在发生此事件时触发。我们选择Rectangle.Loaded事件,这意味着它会在加载时触发;)。
我们添加一个BeginStoryboard属性来开始我们的故事板,并添加一个Storyboard。此动画将在Opacity属性上使用DoubleAnimation,这意味着在5秒的持续时间内,不透明度将逐渐淡化为零,然后返回(AutoReverse属性),它将永远执行此操作(RepeatBehaviour属性)。
答案 1 :(得分:2)
<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>