如何从列表框中检索所选图像并将其保存到/来自lonelystorage

时间:2012-10-06 18:53:40

标签: c# windows-phone-7 isolatedstorage

我正在将图像从媒体库中转发到包装面板内的列表框现在我想将所选图像(它的多选列表框)保存到孤立存储区。

列表框的xaml

    <ListBox Name="vaultbox" SelectionMode="Multiple" 
                ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
     <TextBlock Text="It is so lonely here..." Visibility="Collapsed" />
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel ItemWidth="200" ItemHeight="200"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image Name="image2" 
                               Stretch="Fill" 
                               VerticalAlignment="Top" Source="{Binding}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我迷失在这里。我试图这样做。

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

        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;

使用上面的代码我得到了这个例外

  

'System.Invalid.Operation.Exception Items集合必须为空   在使用ItemsSource'

之前

不知道为什么它的代码差不多就是我从媒体库到列表框的图片。

也是从上面的类似列表框但不同的我尝试将文件保存到isolatedstorage但我似乎可以找出如何获取图像名称... 看这里。目前我正在使用“名称”我能为此做些什么?

foreach (BitmapImage item in lstImageFromMediaLibrary.SelectedItems)
{     
    string filepath =System.IO.Path.Combine("images", "name");
    IsolatedStorageFileStream ifs = fileStorage.CreateFile(filepath);
            {
                var bmp = new WriteableBitmap(item);
                bmp.SaveJpeg(ifs,item.PixelWidth,item.PixelHeight,0,90);
            }

        }

2 个答案:

答案 0 :(得分:0)

我建议你使用MVVM模式:创建ViewModel并将集合属性放在里面。此外,在此ViewModel类中使用“selected item”属性并将ListBox的选定项绑定到它上非常方便。

似乎有必要引入图像项表示:它是带有名称的图像。

public class ImageViewModel : ViewModelBase
{
    public ImageViewModel(string name, string image)
    {
        Name = name;
        Image = image;
    }

    // Name or Path? Please make the choice by yourself! =)
    public string Name { get; private set; }
    public BitmapImage Image { get; private set; }
}

ViewModelBase class(实现INotifyPropertyChanged接口):

public abstract class ViewModelBase : INotifyPropertyChanged
{
    protected ViewModelBase()
    {
    }

    #region Implementation of INotifyPropertyChanged interface

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            var args = new PropertyChangedEventArgs(propertyName);
            handler(this, args);
        }            
    }

    #endregion
}

主(根)视图模型。

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {            
        // Call this directly:
        // var images = LoadImages();
        // Images = images;

        // or bind the Command property of Button to the LoadImagesCommand.
        LoadImagesCommand = new RelayCommand((param) => 
            {
                var images = LoadImages();
                Images = new ObservableCollection<ImageViewModel>(images);
            });
    }

    private ObservableCollection<ImageViewModel> _images;
    public ObservableCollection<ImageViewModel> Images
    {
        get { return _images; }
        private set
        {
            if (value == _images)
                return;
            _images = value;
            RaisePropertyChanged("Images");
        }
    }

    private ImageViewModel _selectedImage;
    public ImageViewModel SelectedImage
    {
        get { return _selectedImage; }
        set
        {
            if (value == _selectedImage)
                return;
            _selectedImage = value;
            RaisePropertyChanged("SelectedImage");
        }
    }

    public ICommand LoadImagesCommand { get; private set; }

    private List<ImageViewModel> LoadImages()
    {
        List<ImageViewModel> images = new List<ImageViewModel>();

        // Part of the original code.
        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);

                ImageViewModel imageViewModel = new ImageViewModel(fileName, bitmapImage);
                images.Add(imageViewModel);
            }
        }
    }
}

窗口。

public class MainView : Window
{
    public MainView()
    {
        InititializeComponent();
        // It is just for simplicity. It would be better to use MVVM Light framework: ViewModel Locator!
        DataContext = new MainViewModel();
    }
}

XAML:

<!-- Please correct the binding for the Image property inside the ItemStyle -->
<ListBox Name="vaultbox"
         ItemsSource="{Binding Images}"
         SelectedItem={Binding SelectedImage}"
         ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
         Height="493"
         HorizontalAlignment="Left"
         Margin="0,0,0,0"
         VerticalAlignment="Top"
         Width="442"
         SelectionMode="Multiple">
    ...
</ListBox>

另外,请考虑异步加载:它不会冻结UI。

答案 1 :(得分:0)

由于这一行而发生异常:

  <TextBlock Text="It is so lonely here..." />

你有一个列表框的孩子。并且您尝试添加更多。