Как получить изображение из изолированного хранилища - PullRequest
2 голосов
/ 05 мая 2011

У меня есть этот 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 ...)?

Ответы [ 2 ]

3 голосов
/ 05 мая 2011

В вашем классе 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));
            };
0 голосов
/ 05 мая 2011

Вот очень полный пример того, как писать и читать из хранилища ISO.

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

...