在屏幕的一部分上移动图像的最佳方式?
我的页面上有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用情节提要 - 这是硬件加速的,并且所有操作都发生在渲染线程上,因此您会看到比尝试直接一遍又一遍地更新位置更好的性能。
故事板的优点是基于时间而不是基于帧,因此很容易声明“我希望图像在 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.
Thread.Sleep 将冻结所有 UI 处理,使用 Dispatcher 类。
Thread.Sleep will freeze ALL UI processing, use Dispatcher class.