如何从lonelystorage中删除图像

时间:2012-10-07 16:33:07

标签: c# windows-phone-7 windows-phone-7.1 windows-phone isolatedstorage

我有一个列表框,它绑定到从隔离存储中获取的图像,这是我如何获取这些图像......

     foreach (string fileName in fileStorage.GetFileNames("images//*.*"))
            {
                if (fileName == null)
                    break;
                string filepath = System.IO.Path.Combine("images", fileName);
                using (IsolatedStorageFileStream imageStream = fileStorage.OpenFile(filepath, FileMode.Open, FileAccess.Read))
                {
                    var imageSource = PictureDecoder.DecodeJpeg(imageStream);
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(imageStream);
                    vltBitmapImage.Add(bitmapImage);

                }
            }
          this.vaultbox.ItemsSource = vltBitmapImage;

现在我要删除所选图像(可以选择多个)

     if (vaultbox.SelectedItems.Count != 0)
        {
            MessageBoxResult m = MessageBox.Show("Files will be deleted forever", "Are you sure?", MessageBoxButton.OKCancel);
            if (m == MessageBoxResult.Cancel)
                vaultbox.SelectedIndex = -1;
            else if(m==MessageBoxResult.OK)
            {
                foreach (BitmapImage item in vaultbox.SelectedItems)
                {
                    //what should i do here?
                }
            }
        }
        else
            MessageBox.Show("No file selected");

1 个答案:

答案 0 :(得分:0)

您必须存储连接图像文件名。

例如:

public class ImageToName
{
   public BitmapImage Image {get;set;}
   public string FileName {get;set;}
}

列表将更改为:

List<ImageToName> vltBitmapImage = new List<ImageToName>();

以这种方式填写列表:

 //read file
 var imageSource = PictureDecoder.DecodeJpeg(imageStream);
 BitmapImage bitmapImage = new BitmapImage();
 bitmapImage.SetSource(imageStream);
 var item = new ImageToName {Image = bitmapImage , Name = fileName };
 vltBitmapImage.Add(item);

您需要更改ListBox声明

 <ListBox.ItemTemplate>
   <DataTemplate>
   <Image Source={Binding Image}/>
   </DataTemplate>
 </ListBox.ItemTemplate>

因此,您将能够从所选项目中获取文件名。