2012-02-08 37 views
2

我在我的應用程序中從獨立存儲設置加載圖像路徑。將圖像從獨立存儲加載到可觀察集合不起作用

[DataMember] 
    public string entryImage = ""; 

    [DataMember] 
    public string EntryImage 
    { 
     get { return entryImage; } 
     set { entryImage = value; } 
    } 

使用助手類將圖像存儲到獨立的存儲文件中。

public static void SaveImage(Stream imageStream, string directory, string filename) 
    { 
     try 
     { 
      string path = System.IO.Path.Combine(directory, filename); 

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       if (!isoStore.DirectoryExists(directory)) isoStore.CreateDirectory(directory); 

       using (var writeStream = isoStore.CreateFile(path)) 
       { 
        byte[] buffer = new byte[32768]; 
        while (true) 
        { 
         int read = imageStream.Read(buffer, 0, buffer.Length); 

         if (read <= 0) 
          return; 
         writeStream.Write(buffer, 0, read); 
        } 
       } 
      } 

     } 
     // Catch exception if unable to save the image 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

這裏是我存放的ImagePath到的ObservableCollection

 MyDiaryItem _saveItems = new MyDiaryItem(); 
     _saveItems.EntryNotes = InputText.Text; 
     _saveItems.EntryDate = date.ToString(); 
     _saveItems.EntryImage = AppHelper.ImageDirectory + AppSettings.ImageFilename; 

的部分在哪裏MyDiaryItem是觀察集合

public ObservableCollection<MyDiaryItem> diaryItems = null; 

獨立存儲保存和載入

  void LoadSettings() 
    { 
     if (settings.Contains("DiaryItems")) 
     { 
      diaryItems = new ObservableCollection<MyDiaryItem>((List<MyDiaryItem>)settings["DiaryItems"]); 
     } 
    } 

    void SaveSettings() 
    { 
     //settings["DiaryItems"] = diaryItems.ToList(); 
     if (diaryItems.ToList() != null) 
     { 
      settings.Clear(); 
      settings.Add("DiaryItems", diaryItems.ToList()); 
      settings.Save(); 
     } 
    } 

下面是圖片來源

  <ListBox toolkit:TiltEffect.IsTiltEnabled="true" Name="AllEntriesList" 
        Margin="0,0,-12,0" 
        SelectionChanged="AllEntriesList_SelectionChanged"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" Margin="0,0,0,17"> 
          <Image Source="{Binding EntryImage}" Height="100" Width="100" Stretch="Fill" Margin="12,0,9,0" /> 
          <StackPanel Margin="0,0,0,17" Width="350" Height="Auto"> 
           <TextBlock Text="{Binding EntryLocation}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" /> 
           <TextBlock Text="{Binding EntryNotes}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" /> 
           <TextBlock Text="{Binding EntryDate}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" /> 
          </StackPanel> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

的XAML代碼我想一些如何使用來自獨立存儲retrived以顯示diaryitems列表中的圖像的ImagePath。

我在我的OnNavigatedTo函數中加載所有的日記文件就像這樣。

AllEntriesList.ItemsSource = app.diaryItems;

我可以在diaryItems列表中看到正確填充的圖像名稱。我想在diaryItems列表中顯示圖像。怎麼做 ?

+1

@MyKuLLSKI - 請不要編輯問題,說它可能是重複的。這是結束或評論的目的。 – 2012-02-08 15:00:05

+0

@Erno - 請告訴我一個引用這條規則的鏈接。謝謝 – MyKuLLSKI 2012-02-08 15:25:43

+0

http://meta.stackexchange.com/questions/121652/adding-possible-duplication-above-original-question – 2012-02-08 15:51:09

回答

1
<Image Source="{Binding EntryImage}" Height="100" Width="100" Stretch="Fill" Margin="12,0,9,0" /> 

你正在綁定一個字符串到圖像源。嘗試將其綁定到BitmapSource
您可以輕鬆地從流中獲取BitmapSource。例如:

BitmapSource CreateSource(Stream stream) 
{ 
    return source = PictureDecoder.DecodeJpeg(stream); 
}