【问题标题】:How get image from isolated storage如何从隔离存储中获取图像
【发布时间】:2011-05-05 15:27:48
【问题描述】:

我有这个 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...) 的图像?

【问题讨论】:

    标签: c# silverlight windows-phone-7 isolatedstorage


    【解决方案1】:

    在您的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;
    }
    

    然后在你的代码sn-p中:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多