1

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

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

  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 应用商店应用中尝试了类似的东西:

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

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

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

谢谢!

4

2 回答 2

3

如果您SetSourceAsync在 BitmapImage 上使用该方法并在将其附加到图像源之前等待它,您应该不会看到闪烁:-

// Ensure the stream is disposed once the image is loaded
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
    // Set the image source to the selected bitmap
    BitmapImage bitmapImage = new BitmapImage();
    await bitmapImage.SetSourceAsync(fileStream);
    imageSource  = bitmapImage;
}

MSDN文档对此有更多信息

于 2013-01-09T08:24:46.057 回答
0

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

于 2013-01-11T18:27:25.750 回答