【发布时间】:2009-04-22 16:47:39
【问题描述】:
我有一些从this post on MSDN 修改的滚动文本动画。不过,我有两个问题。
首先是我需要能够定期更新文本。但是,当 OnTick() 触发时,我收到以下错误,“调用线程无法访问此对象,因为不同的线程拥有它。”我尝试了几种不同的方法,并发布了我尝试过的一种方法。
第二个是不是来回滚动,我真的需要文本表现得像一个真正的选框并朝一个方向移动,内容不断流动,没有间隙,即“abcdeabcde ...”而不是“abcde” .这是否需要两个故事板与相同的文本串联运行,还是有其他方法可以实现?
Storyboard storyboard = new Storyboard();
Timer timer;
public void OnLoad(object sender, RoutedEventArgs e)
{
_presenter.OnViewReady();
StartMarquee();
}
public MyControl()
{
InitializeComponent();
Loaded += OnLoad;
timer = new Timer(OnTick, null, 10000, 10000);
}
private void OnTick(object state)
{
storyboard.Stop(marqueeText);
storyboard = new Storyboard();
marqueeText.Text =
"Fusce id massa sed tortor volutpat viverra. Mauris ut quam. Fusce iaculis magna at urna. In sed dui vitae quam faucibus ullamcorper. Donec hendrerit magna eget neque. Mauris sit amet risus dictum mauris ultricies ornare. Phasellus lectus leo, mattis eget, ultrices vel, suscipit eu, tellus. Integer ut enim. Suspendisse hendrerit mattis sem. Aenean interdum elementum libero. ";
StartMarquee();
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
marqueeText.Text =
"Is it possible to create a marquee or scrolling text in WPF? Is it possible to create a marquee or scrolling text in WPF? Is it possible to create a marquee or scrolling text in WPF? Is it possible to create a marquee or scrolling text in WPF? Is it possible to create a marquee or scrolling text in WPF? Is it possible to create a marquee or scrolling text in WPF?";
}
private void StartMarquee()
{
var canvas = CommonFunctions.FindVisualParent<Canvas>(marqueeText);
if (marqueeText.ActualWidth < canvas.ActualWidth) return;
var duration = new Duration(TimeSpan.FromSeconds(marqueeText.ActualWidth / 60));
var animation = new DoubleAnimation(-marqueeText.ActualWidth, canvas.ActualWidth, duration);
animation.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTargetName(animation, "rtTTransform");
animation.AutoReverse = false;
Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.XProperty));
storyboard.Children.Add(animation);
storyboard.Begin(marqueeText);
}
在视图中,控件声明为
<Canvas Grid.Column="1" HorizontalAlignment="Stretch" ClipToBounds="True" Margin="10,0">
<TextBlock Canvas.Left="0" Canvas.Top="0" x:Name="marqueeText" TextWrapping="NoWrap" VerticalAlignment="Center"
Grid.Column="1" Foreground="{x:Static Brushes.White}" ClipToBounds="False" FontSize="16">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform x:Name="rtTTransform"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Canvas>
在此先感谢,我仍在努力,并将根据我发现的任何更改进行更新。
[已编辑] 移除了 AutoReverse,让事情变得不那么混乱,更接近我想要完成的目标。
【问题讨论】: