2012-10-06 100 views
0

我正在回收圖像從媒體庫到包裝面板內的列表框現在我想將選中的圖像(其多選列表框)保存到孤立的存儲。如何retrived並將選定圖像保存到/從列表框/從列表框保存到/從isolatedstorage

列表框

<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> 

的XAML我在這裏丟失。我正在嘗試這樣做。

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

     } 

回答

0

異常occures因爲這行:

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

你有ListBox中的孩子。你嘗試添加更多。

+0

thanx man that it ..... – goldsmit409

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類(實現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。

+0

我不知道mvvm只是現在在那個時候你可以告訴我每個代碼片段究竟在哪裏? 我也需要爲ViewModelBase添加一些引用類嗎? 和異步加載,如果你的意思是通過BackgroundWorker加載,那麼是的,我使用的是...但仍然發現移動通過全景頁面有點慢,當有相當多的圖像..... – goldsmit409

+0

好的,請參閱最新的答案:已添加'ViewModelBase'類,'MainViewModel'類已更新以使用它。 –

+0

窗戶在哪裏....那是公共類mainview:Window ....部分代碼去了? – goldsmit409

相關問題