问题描述
我有一个WPF应用程序,该应用程序在页面底部有一个文本块.我想为该块设置动画,使其沿底部滚动.我需要在代码中执行此操作,以便当其滚动屏幕时,当画布的宽度小于0时,它将改变位置到另一侧-其宽度,因为此时我也更改了文本.这将循环.我在带有演示图板的Silverlight应用程序中做了完全相同的事情,它运行得很好.我使用的代码如下:
I have a WPF application which has a textblock at the bottom of the page. I want to animate this block so it scrolls along the bottom. I need to do this in code so that when it scrolls of screen it will change position to the other side when it''s canvas is less than 0 - it''s width as I also change the text at this point. This will loop. I did the exact same thing in a silverlight application with a storyboard and it worked perfectly. The code I used is as follows:
private Storyboard _newsScrollTimer = new Storyboard();
_newsScrollTimer.Duration = TimeSpan.FromMilliseconds(0);
_newsScrollTimer.Completed += new EventHandler(Animation);
_newsScrollTimer.Begin();
private void Animation(object sender, EventArgs e)
{
try
{
//Moves the news item using its canvas property.
NewsItem.SetValue(Canvas.LeftProperty, (double)NewsItem.GetValue(Canvas.LeftProperty) - 1.5);
_newsScrollTimer.Begin();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
在我的Silverlight应用程序中,这工作得很好,但是在WPF中根本无法工作.如果将时间跨度值更改为1,它将工作几秒钟,然后突然停止.每次停在不同的地方.如何修改此代码,使其适用于WPF.另外,您能告诉我另一种平滑文本块动画的方法吗?我尝试使用调度计时器代替它,但是它的动画在某些地方比较生涩.在此先感谢您.
This works perfectly fine in my silverlight app, however it won''t work at all in WPF. If I change the timespan value to 1 it will work for a few seconds and then stop suddenly. It stops at a different place each time. How do I modify this code so it will work for WPF. Alternatively could you tell me another way to animate a textblock smoothly. I tried using a dispatch timer instead which works, but the animation is jerky in places. Thanks in advance.
推荐答案
这篇关于WPF故事板播放然后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!