Xaml 如何设置按钮的背景而不闪烁?

Xaml 如何设置按钮的背景而不闪烁?,xaml,windows-8,windows-runtime,Xaml,Windows 8,Windows Runtime,我正在尝试将按钮的背景更改为图像源。我想在导航到页面时将该图像加载到内存中,以便它在第一次显示时不会闪烁 在Windows Phone上,我可以创建如下图像源: StreamResourceInfo resourceInfo = Application.GetResourceStream(uri); BitmapImage bitmapSource = new BitmapImage(); // Avoid flicker by not delay-loading. bitma

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

在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;
但同样的问题也出现了。按钮已经有一个不同的图像作为背景,在某个特定事件中,我希望它更改为新的背景。但当我改变光源时,观察到明显的闪烁。我假设这是因为图像还没有在内存中,因为问题在第二次修改图像源时就消失了

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


谢谢

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

// 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;
}

文档中有更多关于这方面的信息

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

谢谢。您确定这对资源位图是个好主意吗?我只是担心我会绕过为资源提供的自动位图缓存,特别是因为我会有十几个左右的按钮使用相同的位图。