0

我正在将媒体库中的图像检索到包装面板内的列表框中,现在我想将所选图像(它是一个多选列表框)保存到隔离存储中。

列表框的xml

    <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 之前必须为空'

不知道为什么它的代码几乎与我从媒体库显示图片到列表框的代码相同。

也从上面的类似列表框中,但不同的列表框中,我尝试将文件保存到隔离存储,但我似乎可以找出如何获取图像名称......请参见此处。目前正在使用“名称”我该怎么办?

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

        }
4

2 回答 2

0

我想推荐您使用 MVVM 模式:创建 ViewModel 并将集合属性放入其中。此外,在这个 ViewModel 类中拥有“选定项”属性并将选定项绑定到它非常方便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类(实现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。

于 2012-10-06T19:42:05.430 回答
0

由于这一行而发生异常:

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

你有一个 ListBox 的孩子。你尝试添加更多。

于 2012-10-06T19:45:40.857 回答