Silverlight、XAML实现滚动文字

主要是用到DoubleAnimation 标签:

 

<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.Left)" From="1024" To="0" Duration="0:0:15"  />
                        </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"   //从右往左滚动

 滚动文字的暂停也很简单,可以在后台用代码控制:

animation.Pause();//暂停
animation.Resume();//恢复

 

posted @ 2012-12-04 16:28  Gyoung  阅读(1080)  评论(0编辑  收藏  举报