WPF故事板播放然后停止 [英] WPF storyboard plays then stops

查看:87
本文介绍了WPF故事板播放然后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

推荐答案

您可以从Microsoft安装Expression Blend,然后在XAML故事板上完成所有操作.我在WPF中使用XAML直接完成了类似的事情,并取得了良好的成功.您获得的XAML代码将是一个很好的示例,然后您可以对其进行手动调整.
You could install Expression Blend from Microsoft and then do it all in a XAML storyboard. I have done things like this in WPF using the XAML directly with good success. The XAML code you get will be a good example and then you can tweak it manually.


这篇关于WPF故事板播放然后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆