我以前的工作位图代码在Windows RT上工作时遇到类似的问题,早期的尝试说服我拒绝下载任何东西,除非它将显示在UI上(这里,我需要在分配前插入1ms延迟小号只是OURCES得到它的触发图像下载):
var image = .... // reference to animage on the UI
var placeholder = ... // a placeholder BitmapImage
var source = ... // uri to download
image.Source = placeholder;
var src = new BitmapImage(new Uri(source));
src.ImageOpened += (s, e) =>
{
var bi = s as BitmapImage;
image.Source = bi;
};
image.Source = src;
// Delay required to trigger download
await Task.Delay(1);
image.Source = placeholder;
这里是另一种解决办法我已经成功尝试:
var image = .... // reference to animage on the UI
var source = ... // uri to download
var placeholder = ... // a placeholder BitmapImage
image.Source = placeholder;
var bytes = await new HttpClient().GetByteArrayAsync(source);
var img = new BitmapImage();
await img.SetSourceAsync(bytes.AsBuffer().AsStream().AsRandomAccessStream());
image.Source = img;
WinRT的文档是很不够的现在。但请记住,任何可能需要超过50毫秒的*任何*都需要异步完成。看看你从ImageOpened事件中得到了什么。不要忘记ImageFailed。 – 2012-03-03 13:10:11
Casn您详细阐述了如何在运行时获取文件。这是通过FilePicker吗?当你的意思是静态内容时,这个内容是你应用程序的一部分吗? – 2012-03-03 17:55:09