2012-02-08 114 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); 
}