延迟加载位图图像

本文关键字:图像 位图 延迟加载 | 更新日期: 2023-09-27 17:57:03

我正在尝试在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;

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

延迟加载位图图像

听起来可能是

设置源后视图未刷新的内容。 加载空列表框。 然后加载所有图像。什么都没显示出来! 但是当你回到页面时,魔法!它们都在那里,因为整个视图被重新绘制(现在使用图像源)。 如果在返回应用之前终止应用,则应再次看到一个空列表框,因为必须重置源。 我不确定这在 Windows 8 中是如何工作的,但您应该能够调用类似列表框的东西。RefreshView() 强制它重绘自身。

我实际上需要调用 ViewModel 中的属性RaisePropertyChanged来强制更新,如此处所述,但您的回答为我指明了正确的方向。谢谢@teabaggs。

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

使用这个:图片。CreateOptions = BitmapCreateOptions.BackGround;

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