在屏幕的一部分上移动图像的最佳方式?

发布于 2024-10-10 06:47:57 字数 522 浏览 0 评论 0原文

我的页面上有一个 Silverlight WP7 应用程序和一个图像,我希望该图像在屏幕上滑动。这样做的最佳方法是什么?我写得很快,但直到整个方法完成后 UI 才会更新。

    private void SpinImg(Image img, double left) {
        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);
        }
    }

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 left) {
        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);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩心态 2024-10-17 06:47:57

使用情节提要 - 这是硬件加速的,并且所有操作都发生在渲染线程上,因此您会看到比尝试直接一遍又一遍地更新位置更好的性能。

故事板的优点是基于时间而不是基于帧,因此很容易声明“我希望图像在 0.5 秒内从 移动到 到”并且它就会发生。

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.

如痴如狂 2024-10-17 06:47:57

Thread.Sleep 将冻结所有 UI 处理,使用 Dispatcher 类。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文