延迟加载BitmapImage

时间:2013-02-02 18:19:29

标签: c# .net windows-phone-8 windows-phone bitmapimage

我正在尝试在Windows Phone 8中的ListBox中加载BitmapImages并看到奇怪的行为。第一次显示页面时,ListBox显示为空,但如果我返回,则返回页面加载图像。对我来说,好像图像在需要时没有被加载。这是我正在使用的代码(通过转换器):

        BitmapImage img = null;
        StreamResourceInfo res = Application.GetResourceStream(new Uri("/MyAssembly;component/Resource/images/myimage.png", UriKind.Relative));
        Stream s = res.Stream;
        using (s) {
            img= new BitmapImage();
            img.SetSource(s);
        }
        img.CreateOptions = BitmapCreateOptions.None;

我尝试过尝试使用创建选项无济于事。

3 个答案:

答案 0 :(得分:1)

听起来它可能是在设置源之后视图没有被刷新的东西。您加载一个空列表框。然后加载所有图像。 arrggh没什么特别的!但当你回到页面,魔术!他们都在那里因为整个视图被重绘(现在使用图像源)。如果您在返回应用程序之前终止应用程序,则应再次看到一个空列表框,因为必须重置源。我不确定它在Windows 8中是如何工作的,但你应该可以调用类似listbox.RefreshView()来强制它重绘自己。

答案 1 :(得分:0)

我实际上需要在我的ViewModel中的属性上调用RaisePropertyChanged来强制更新,as described here但是你的答案指向了正确的方向。谢谢@teabaggs。

奇怪的是,当我将Pivot控件作为根元素时,这很有用,但切换到Panorama必须更改事件顺序。

答案 2 :(得分:0)

使用此: img.CreateOptions = BitmapCreateOptions.BackGround;

并分配 private void BitmapImage_ImageFailed(object sender, ExceptionRoutedEventArgs e) { BitmapImage img = new BitmapImage(); img.UriSource = new Uri("Your local Image", UriKind.Relative); } 你的本地形象。 无论如何图像需要时间渲染,这是我迄今为止找到的最好和最快的方法