Windows phone 7 从wp7中的独立存储中检索多个映像

Windows phone 7 从wp7中的独立存储中检索多个映像,windows-phone-7,isolatedstorage,Windows Phone 7,Isolatedstorage,我正在尝试使用listbox从独立存储中检索多个图像,但我不确定为什么它只从独立存储中检索最新的图像。因此,希望任何人都能帮助我修改代码,或者提供与我的代码大致相同的示例代码。谢谢 我的代码: private void LoadFromLocalStorage(string imageFolder, string imageFileName ) { var isoFile = IsolatedStorageFile.GetUserStoreForApplication(); if (

我正在尝试使用listbox从独立存储中检索多个图像,但我不确定为什么它只从独立存储中检索最新的图像。因此,希望任何人都能帮助我修改代码,或者提供与我的代码大致相同的示例代码。谢谢

我的代码:

private void LoadFromLocalStorage(string imageFolder, string imageFileName )
{ 

  var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
  if (!isoFile.DirectoryExists(imageFolder))

{
  isoFile.CreateDirectory(imageFolder);
}

  string filePath = Path.Combine(imageFolder, imageFileName);
  using (var imageStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
{
  var imageSource = PictureDecoder.DecodeJpeg(imageStream);
  BitmapImage bi = new BitmapImage();
  ListBoxItem item = new ListBoxItem();
  bi.SetSource(imageStream);
  item.Content = new Image() 
{ 
  Source = bi, MaxHeight = 100, MaxWidth = 100 };
  listBox1.Items.Add(item);

}

如果你能告诉我你得到了什么样的结果,那会很有帮助

  • 你只看到一个元素吗
  • 您是否看到多个元素并且它们是相同的
  • 您是否看到多个元素,其中只有一个显示图片,而其他元素为空
无论如何,这不是处理listbox的正确方法。但首先要做的是。 这行没有做任何有用的事情:

var imageSource = PictureDecoder.DecodeJpeg(imageStream);
这段代码应该可以工作(看起来),但代码之外可能有错误。这个函数被调用了多少次,传递了哪些参数——这才是真正重要的

但是我会修改代码,使用数据绑定和适当的ItemsSource

  • 为项目创建类

    public class MyImage 
    { 
        public string FilePath {get; set;} 
        public ImageSource LoadedSource {get; set;} 
    }
    
  • 创建一个ObservableCollection()并用数据填充它

  • 通过设置ItemsSource将其绑定到ListBox
  • 使用图像和绑定设计适当的ItemTemplate:

    <Image Source={Binding LoadedSource}/>
    
    
    

  • 此设置将帮助您轻松调试问题并定位问题。很可能是您错误地调用了原始函数。

    可能重复了难以告知的w/o查看listbox或ViewModel如何调用LoadFromLocalStorage