Windows phone 7 将数据从隔离存储加载到列表集合失败
我已将近150张高分辨率图像存储到789x1299分辨率的独立存储器中。我的问题是,当我将60-70个图像加载到列表集合中时,它工作正常,但当它超过70个时,bi.SetSource(ms)中会出现内存不足异常。我正在项目模板中使用虚拟化satck面板。原因是什么Windows phone 7 将数据从隔离存储加载到列表集合失败,windows-phone-7,silverlight-4.0,windows-phone-7.1,Windows Phone 7,Silverlight 4.0,Windows Phone 7.1,我已将近150张高分辨率图像存储到789x1299分辨率的独立存储器中。我的问题是,当我将60-70个图像加载到列表集合中时,它工作正常,但当它超过70个时,bi.SetSource(ms)中会出现内存不足异常。我正在项目模板中使用虚拟化satck面板。原因是什么 List<SampleData> data = new List<SampleData>(); try { for (int i = 0
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;
}
}
}
列表数据=新列表();
尝试
{
对于(int i=0;i<150;i++)
{
字节[]数据2;
使用(IsolatedStorageFile isf=IsolatedStorageFile.GetUserStoreForApplication())
{
使用(IsolatedStorageFileStream isfs=isf.OpenFile(“IMAGES”+i+“.jpg”、FileMode.Open、FileAccess.Read))
{
data2=新字节[isfs.Length];
读取(data2,0,data2.Length);
isfs.Close();
}
}
MemoryStream ms=新的MemoryStream(数据2);
BitmapImage bi=新的BitmapImage();
bi.SetSource(ms);
Add(new SampleData(){Name=bi});
}
this.list.ItemsSource=数据;
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
公共类抽样数据
{
公共图像源名称
{
得到;
设置
}
}
}
}
我相信,通过首先创建
位图图像,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;
}
}
}
由于您有许多高分辨率图像,您可能仍然会遇到性能问题-我建议将这些图像的低分辨率版本存储在列表框中,然后在用户需要查看特定图像时显示高分辨率图像?希望这有帮助
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;
}
}
}