内存不足在通过文件选择器加载大量图像时出现异常

时间:2014-12-02 09:17:52

标签: c# out-of-memory windows-phone-8.1

Windows Phone 8.1:

foreach (StorageFile file in ImageFiles)
            {

                ClassForFolderImages CFFI = new ClassForFolderImages();
                using(var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                 var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();

                 bitmapImage.DecodePixelWidth = 80;
                 bitmapImage.DecodePixelHeight = 80;
                 await bitmapImage.SetSourceAsync(stream);

                 CFFI.imageForFolder = bitmapImage;

                 GridViewName.Add(CFFI);
                }
            }

在上面的片段" ImageFiles"包含大量图像和" GridViewName"是可观察集合的名称,我稍后将其绑定到网格视图,我希望在其中显示这些图像。 ClassForFolderImages类如下:

public class ClassForFolderImages : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;


    private BitmapImage img;

    public BitmapImage imageForFolder
    {
        get { return img; }

        set
        {
            img = value;
            FirePropertyChangedEvent("imageForFolder");
        }
    }


    public ClassForFolderImages()
    {

    }

    private void FirePropertyChangedEvent(string propertyName)
    {

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

}

这个Snippet在手机模拟器和设备上的少量图像上运行良好,但在设备上 - 随着图像数量的增加,它开始抛出以下错误(它在App.gics中断) :

Exception = The function evaluation was disabled because of an out of memory exception.
Handled = The function evaluation was disabled because of an out of memory exception.
Message = The function evaluation was disabled because of an out of memory exception.

有人可以提出改变来解决这个问题吗?

0 个答案:

没有答案