在Silverlight中实现marquee的效果

在XAML中,添加如下代码:

 

<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" BorderThickness="0" Height="36"> <Canvas Loaded="MarqueeControl_Loaded" Width="592" HorizontalAlignment="Left"> <Canvas.Resources> <Storyboard x:Name="sb"> <DoubleAnimation x:Name="da" BeginTime="00:00:02" Storyboard.TargetName="txtLorem" Storyboard.TargetProperty="(Canvas.Left)" From="0" RepeatBehavior="Forever"/> </Storyboard> </Canvas.Resources> <TextBlock x:Name="txtLorem" Width="596" Height="33" Text="滚动文字内容..."/> </Canvas> </ScrollViewer>

 

在c#里创建相应的方法:

 

void MarqueeControl_Loaded(object sender, RoutedEventArgs e) { var canvas = sender as Canvas; if (txtLorem.ActualWidth <= canvas.Width) return; const double speed = 50; da.To = -txtLorem.ActualWidth; da.Duration = new Duration(TimeSpan.FromSeconds(txtLorem.ActualWidth / speed)); sb.Begin(); }

窗体加载的时候:

给把要显示的文字赋值给txtLorem: this.txtLorem.Text = "滚动文字。。。。。";

 

你可能感兴趣的:(object,C#,silverlight)