Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 2.07 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Load data to a list collection from isolated storage fails
  2. List<SampleData> data = new List<SampleData>();
  3.         try
  4.         {
  5.  
  6.             for (int i = 0; i < 150; i++)
  7.             {
  8.  
  9.                 byte[] data2;
  10.  
  11.                 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
  12.                 {
  13.  
  14.                     using (IsolatedStorageFileStream isfs = isf.OpenFile("IMAGES" + i + ".jpg", FileMode.Open, FileAccess.Read))
  15.                     {
  16.                         data2 = new byte[isfs.Length];
  17.                         isfs.Read(data2, 0, data2.Length);
  18.                         isfs.Close();
  19.                     }
  20.  
  21.                 }
  22.  
  23.  
  24.                 MemoryStream ms = new MemoryStream(data2);
  25.  
  26.                 BitmapImage bi = new BitmapImage();
  27.  
  28.                 bi.SetSource(ms);
  29.  
  30.                 data.Add(new SampleData() { Name = bi });
  31.  
  32.  
  33.  
  34.             }
  35.             this.list.ItemsSource = data;
  36.  
  37.         }
  38.         catch(Exception ex)
  39.         {
  40.             MessageBox.Show(ex.ToString());
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.         }
  47.  
  48.  
  49.  
  50.     public class SampleData
  51.     {
  52.         public ImageSource Name
  53.         {
  54.             get;
  55.             set;
  56.         }
  57.  
  58.  
  59.     }
  60. }
  61.        
  62. <ListBox x:Name="list" Width="480">
  63.             <ListBox.ItemTemplate>
  64.                 <DataTemplate>
  65.                     <StackPanel Margin="10">
  66.  
  67.                         <Image Height="500" Width="500" Source="{Binding Name}" />
  68.                     </StackPanel>
  69.                 </DataTemplate>
  70.             </ListBox.ItemTemplate>
  71.             <ListBox.ItemsPanel>
  72.                 <ItemsPanelTemplate>
  73.                     <VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" Orientation="Vertical"/>
  74.  
  75.                 </ItemsPanelTemplate>
  76.             </ListBox.ItemsPanel>
  77.         </ListBox>
  78.        
  79. public class SampleData
  80. {
  81.     public byte[] ImgData {get;set;}
  82.     public BitmapImage Image
  83.     {
  84.         get
  85.         {
  86.             BitmapImage bi = new BitmapImage();
  87.             MemoryStream ms = new MemoryStream(ImgData);
  88.             bi.SetSource(ms);
  89.             return bi;
  90.         }
  91.     }
  92. }