SL中利用StoryBoard实现类似html中的Marquee滚动效果,以下是核心代码:
<Canvas x:Name="c1" Margin="2,25,2,2"> <Canvas.Triggers> <EventTrigger RoutedEvent="Canvas.Loaded" > <BeginStoryboard> <Storyboard x:Name="animation" RepeatBehavior="Forever" > <DoubleAnimation Storyboard.TargetName="ScrollingUp" Storyboard.TargetProperty="(Canvas.Top)" From="120" To="0" Duration="0:0:10" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Canvas.Triggers> <TextBlock x:Name="ScrollingUp" FontWeight="Bold" Foreground="Red" FontSize="12" TextWrapping="Wrap" Height="113"> 从下往上滚动的文字 </TextBlock> </Canvas>
这里是滚动文字的代码,主要的就是StoryBoard里的属性设置:
Storyboard.TargetProperty="(Canvas.Top)" From="100" To="0" Duration="0:0:10"
同理,其他方向如下设置即可:
Storyboard.TargetProperty="(Canvas.Top)" From="0" To="100" Duration="0:0:10" //从上往下滚动
Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" Duration="0:0:10" //从左往右滚动
Storyboard.TargetProperty="(Canvas.Left)" From="100" To="0" Duration="0:0:10" //从右往左滚动