简体   繁体   English

如何从isolatedstorage中删除图像

[英]how can i delete a image from isolatedstorage

i have a listbox which is binded to images it gets from isolated storage here is how i get those images... 我有一个列表框绑定到它从隔离存储中获取的图像,这是我获取那些图像的方式...

     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;

now i want to delete the selected image(multiple can be selected) 现在我要删除所选的图像(可以选择多个)

     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");

You have to store a connection Image-File name. 您必须存储连接图像文件名。

For example: 例如:

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

A list will be changed to: 列表将更改为:

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

And fill the list in this way: 并以这种方式填写列表:

 //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);

and you need to change ListBox Declaration 并且您需要更改ListBox声明

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

So, you will be able to get File names from selected items. 因此,您将能够从所选项目中获取文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM