从隔离存储加载数据到列表集合失败

时间:2012-06-01 10:24:58

标签: windows-phone-7 silverlight-4.0 windows-phone-7.1

我已将近150张高分辨率图像存储到789x 1299分辨率的隔离存储中。我的问题是当我将60-70个图像加载到列表集合中时它工作正常但是当它超过70个bi.SetSource(ms)中出现内存不足异常时。我在项目模板中使用虚拟化satck面板。是什么原因

        List<SampleData> data = new List<SampleData>();
        try
        {

            for (int i = 0; i < 150; i++)
            {

                byte[] data2;

                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {

                    using (IsolatedStorageFileStream isfs = isf.OpenFile("IMAGES" + i + ".jpg", FileMode.Open, FileAccess.Read))
                    {
                        data2 = new byte[isfs.Length];
                        isfs.Read(data2, 0, data2.Length);
                        isfs.Close();
                    }

                }


                MemoryStream ms = new MemoryStream(data2);

                BitmapImage bi = new BitmapImage();

                bi.SetSource(ms);

                data.Add(new SampleData() { Name = bi });



            }
            this.list.ItemsSource = data;

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());

        }



        }



    public class SampleData
    {
        public ImageSource Name
        {
            get;
            set;
        }


    }
}

}

      <ListBox x:Name="list" Width="480">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="10">

                        <Image Height="500" Width="500" Source="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" Orientation="Vertical"/>

                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

1 个答案:

答案 0 :(得分:1)

我相信首先创建BitmapImages,WP7会预先为每个图像分配内存,而不是在列表框中的项目滚动到视图中时。尝试在BitmapImage类中存储字节数组而不是SampleData,然后在通过属性调用时创建BitmapImage

所以SampleData看起来像这样:

public class SampleData
{
    public byte[] ImgData {get;set;}
    public BitmapImage Image
    {
        get
        {
            BitmapImage bi = new BitmapImage();
            MemoryStream ms = new MemoryStream(ImgData);
            bi.SetSource(ms);
            return bi;
        }
    }
}

由于您有许多高分辨率图像,您可能仍会遇到性能问题 - 我可能建议为ListBox存储这些图像的较低分辨率版本,然后在用户需要时显示高分辨率图像查看特定图像?希望这有帮助!