
Untitled
By: a guest on
Jul 22nd, 2012 | syntax:
None | size: 2.07 KB | hits: 12 | expires: Never
Load data to a list collection from isolated storage fails
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>
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;
}
}
}