开发者

Best way to move an image across part of the screen?

开发者 https://www.devze.com 2023-02-02 15:12 出处:网络
I have a Silverlight WP7 app and an image on my page that I want to appear to slide across the screen. What is the best way of doing this? I wrote this real quick but the UI doesn\'t update until the

I have a Silverlight WP7 app and an image on my page that I want to appear to slide across the screen. What is the best way of doing this? I wrote this real quick but the UI doesn't update until the entire method is done.

    private void SpinImg(Image img, double le开发者_如何学运维ft) {
        for(int i = 1; i <= 10000; i++) {
            img.Margin = new Thickness(left, img.Margin.Top + 1, 0, 0);
            if(img.Margin.Top > 314) {
                //move it to the top
                img.Margin = new Thickness(left, -105, 0, 0);
            }
            int wait = 1000 / i;
            Thread.Sleep(wait);
        }
    }


Use a Storyboard - this is hardware-acceleratable, and all occurs on the Render thread, so you'll see much better performance than trying to update position directly over and over.

Storyboard has the advantage of being time-based instead of frame-based, so it's easy to declare "I want the image to move from to in 0.5 seconds" and it will just happen.


Thread.Sleep will freeze ALL UI processing, use Dispatcher class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号