由于歌曲名称可能超过屏幕宽度而显示不下,所以当歌曲名称长度大于屏幕宽度的时候滚动显示。
这里主要用storyboard来完成,主要是silverlight的知识了。
1 <Canvas>
2 <Canvas.Resources>
3 <Storyboard x:Name="sb">
4 <DoubleAnimation x:Name="animation"
5 Storyboard.TargetProperty="(Canvas.Left)"
6 Storyboard.TargetName="SongName"
7 Duration="0:0:9"
8 RepeatBehavior="Forever" />
9 </Storyboard>
10 </Canvas.Resources>
11 <TextBlock x:Name="SongName" FontSize="24" HorizontalAlignment="Center" Margin="0,15,0,0" />
3 <Storyboard x:Name="sb">
4 <DoubleAnimation x:Name="animation"
5 Storyboard.TargetProperty="(Canvas.Left)"
6 Storyboard.TargetName="SongName"
7 Duration="0:0:9"
8 RepeatBehavior="Forever" />
9 </Storyboard>
10 </Canvas.Resources>
11 <TextBlock x:Name="SongName" FontSize="24" HorizontalAlignment="Center" Margin="0,15,0,0" />
12 </Canvas>
cs: 1 /// <summary>
2 /// 歌曲名称滚动
3 /// </summary>
4 private void SongNameMarquee()
5 {
6 this.sb.Stop();
7 double sysWidth = System.Windows.Application.Current.Host.Content.ActualWidth;//屏幕宽度
8 if (sysWidth <= this.SongName.ActualWidth)
9 {
10 animation.From = sysWidth;
11 animation.To = -this.SongName.ActualWidth - (sysWidth - 360) / 2;//360为Canvas宽度
12 this.sb.Begin();
13 }
14 else
15 {//canvas居中
16 this.SongName.SetValue(Canvas.LeftProperty, 180-this.SongName.ActualWidth/2);
17 }
18
19 }
3 /// </summary>
4 private void SongNameMarquee()
5 {
6 this.sb.Stop();
7 double sysWidth = System.Windows.Application.Current.Host.Content.ActualWidth;//屏幕宽度
8 if (sysWidth <= this.SongName.ActualWidth)
9 {
10 animation.From = sysWidth;
11 animation.To = -this.SongName.ActualWidth - (sysWidth - 360) / 2;//360为Canvas宽度
12 this.sb.Begin();
13 }
14 else
15 {//canvas居中
16 this.SongName.SetValue(Canvas.LeftProperty, 180-this.SongName.ActualWidth/2);
17 }
18
19 }
主要是计算几个点麻烦点 其他没什么。