如何设置按钮的背景而不闪烁? [英] How to set Background of a Button without flicker?

查看:28
本文介绍了如何设置按钮的背景而不闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将按钮的背景更改为图像源.当我们导航到页面时,我想将该图像加载到内存中,以便它在第一次显示时不会闪烁.

I am trying to change the Background of a button to an image source. I want to load that image in memory when we navigate to the page so that it doesn't flicker the first time it shows.

在 Windows Phone 上,我可以这样创建图像源:

On Windows Phone, I was able to create the image source as such:

  StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
  BitmapImage bitmapSource = new BitmapImage();

  // Avoid flicker by not delay-loading.
  bitmapSource.CreateOptions = BitmapCreateOptions.None;

  bitmapSource.SetSource(resourceInfo.Stream);

  imageSource = bitmapSource;

我在 Windows 8 Store 应用程序中尝试了类似的操作:

I tried something similar in my Windows 8 Store app:

  BitmapImage bitmapSource = new BitmapImage();
  bitmapSource.CreateOptions = BitmapCreateOptions.None;
  bitmapSource.UriSource = uri;
  imageSource = bitmapSource;

但是出现同样的问题.该按钮已经具有与背景不同的图像,并且在某个事件中,我希望它更改为新背景.但是当我改变源时,观察到明显的闪烁.我假设这是因为图像尚未在内存中,因为第二次修改图像源时问题就会消失.

but the same problem occurs. The button already has a different image as the Background, and on a certain event I would like it to change to the new background. But when I change the source, a noticeable flicker is observed. I'm assuming this is because the image is not yet in memory, as the issue goes away the second time the image source is modified.

有人知道解决办法吗?我需要以某种方式强制加载此图像.

Anyone know a solution? I need to somehow force the loading of this image.

谢谢!

推荐答案

谢谢 Ross,但我最终做的是我使用与上面类似的代码预加载了我需要的六张左右的位图,除了从资源当然.我在页面加载时异步执行此操作,然后当我在按钮背景上设置 ImageSource 时,我使用了已经预加载的位图.这样我就知道我没有为位图的每个实例分配一块新的内存.

Thanks Ross, but what I ended up doing instead is I preloaded the half dozen or so bitmaps I needed by using similar code to what you had above, except from resource of course. I did this asynchronously when the page loaded, and then when I set the ImageSource on the button background, I used the already preloaded bitmaps. That way I know I'm not allocated a new chunk of memory for every instance of the bitmap.

这篇关于如何设置按钮的背景而不闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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