【问题标题】:loading image from isolated storage to observable collection not working将图像从隔离存储加载到可观察集合不起作用
【发布时间】:2012-02-08 14:24:34
【问题描述】:

我正在从我的应用程序中的独立存储设置加载图像路径。

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

这是我将图像路径存储到 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();
        }
    }

这里是图片源的xaml代码

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

我想了解如何使用从独立存储中检索到的图像路径在日记项目列表中显示图像。

我正在像这样在我的 OnNavigatedTo 函数中加载所有日记项目。

AllEntriesList.ItemsSource = app.diaryItems;

我可以在 diaryItems 列表中看到正确填充的图像名称。我想在 diaryItems 列表中显示图像。怎么做 ?

【问题讨论】:

  • @MyKuLLSKI - 请不要编辑问题说它可能是重复的。这就是 close 或 cmets 的用途。
  • @Erno - 请显示引用此规则的链接。谢谢
  • 版主说明 此问题下的评论已被删除,因为它们恶化为比信号更多的噪音。如果您认为某个问题是重复的,请将其标记或投票关闭它,或者如果不确定,请留下指向可能重复的评论。不要编辑原始帖子以建议重复。
  • 如果问题重复,请给我现有问题的链接,以便我找到答案。

标签: c# windows-phone-7


【解决方案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);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多