如何从独立存储中获取图像

发布于 2024-11-05 00:24:43 字数 1436 浏览 0 评论 0原文

有这个 XAML

<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}" Name="list">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                <!--Replace rectangle with image-->
                <Image Height="100" Width="100" Source="{Binding Img}" Margin="12,0,9,0"/>
                <StackPanel Width="311">
                    <TextBlock  Text="{Binding Pos}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我在代码中

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    IsolatedStorageFileStream isoStoreStream = isoStore.OpenFile("chart.xml", FileMode.Open);
    using (StreamReader reader = new StreamReader(isoStoreStream))
    {
        XElement xml = XElement.Parse(reader.ReadToEnd());
        var list = from var in xml.Descendants("Pos")
            select new Single
            {
                Pos = Int32.Parse(var.Attribute("id").Value),
                Img = how read from isolated storage the image id.jpg?
            };

public class Single
{
    public int Pos { set; get; }
    public ??? Img { set; get; }
}

:我已经将图像保存到独立存储中,但问题是:如何从独立存储中读取名称为 id.jpg(1.jpg, 2.jpg, 3.jpg ...)?

I have this XAML

<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}" Name="list">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                <!--Replace rectangle with image-->
                <Image Height="100" Width="100" Source="{Binding Img}" Margin="12,0,9,0"/>
                <StackPanel Width="311">
                    <TextBlock  Text="{Binding Pos}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In the Code:

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    IsolatedStorageFileStream isoStoreStream = isoStore.OpenFile("chart.xml", FileMode.Open);
    using (StreamReader reader = new StreamReader(isoStoreStream))
    {
        XElement xml = XElement.Parse(reader.ReadToEnd());
        var list = from var in xml.Descendants("Pos")
            select new Single
            {
                Pos = Int32.Parse(var.Attribute("id").Value),
                Img = how read from isolated storage the image id.jpg?
            };

public class Single
{
    public int Pos { set; get; }
    public ??? Img { set; get; }
}

I have already saved the images into isolated storage but the problem is: how can I read from isolated storage the image that have names like id.jpg(1.jpg, 2.jpg, 3.jpg...)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-11-12 00:24:43

在您的 Single 类中,Img 属性应为 ImageSource 类型。要设置此属性(从isolatedStorage读取图像),您可以执行以下操作:

private ImageSource getImageFromIsolatedStorage(string imageName)
{
    BitmapImage bimg = new BitmapImage();

    using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = iso.OpenFile(imageName, FileMode.Open, FileAccess.Read))
        {
            bimg.SetSource(stream);
        }
    }
    return bimg;
}

然后在代码片段中:

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    IsolatedStorageFileStream isoStoreStream = isoStore.OpenFile("chart.xml", FileMode.Open);
    using (StreamReader reader = new StreamReader(isoStoreStream))
    {
        XElement xml = XElement.Parse(reader.ReadToEnd());
        var list = from var in xml.Descendants("Pos")
            select new Single
            {
                Pos = Int32.Parse(var.Attribute("id").Value),
                Img = getImageFromIsolatedStorage(string.Format("{0}.jpg", Pos));
            };

In your Single Class the Img property should be of type ImageSource. To set this property (read the image from IsolatedStorage) you can do this:

private ImageSource getImageFromIsolatedStorage(string imageName)
{
    BitmapImage bimg = new BitmapImage();

    using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = iso.OpenFile(imageName, FileMode.Open, FileAccess.Read))
        {
            bimg.SetSource(stream);
        }
    }
    return bimg;
}

Then in your code snippet:

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    IsolatedStorageFileStream isoStoreStream = isoStore.OpenFile("chart.xml", FileMode.Open);
    using (StreamReader reader = new StreamReader(isoStoreStream))
    {
        XElement xml = XElement.Parse(reader.ReadToEnd());
        var list = from var in xml.Descendants("Pos")
            select new Single
            {
                Pos = Int32.Parse(var.Attribute("id").Value),
                Img = getImageFromIsolatedStorage(string.Format("{0}.jpg", Pos));
            };
会发光的星星闪亮亮i 2024-11-12 00:24:43

这是一个关于如何写入和读取 ISO 存储的非常完整的示例。

http://www.codeproject。 com/Articles/38636/Saving-Bitmaps-to-Isolated-Storage-in-Silverlight-.aspx

Here is a very complete example of how to write to and read from ISO storage.

http://www.codeproject.com/Articles/38636/Saving-Bitmaps-to-Isolated-Storage-in-Silverlight-.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文