English 中文(简体)
如何从孤立的储存中获得形象
原标题:How get image from isolated storage

<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...)的形象?

问题回答

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

随后,在你的法典中:

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

这里是一个非常完整的实例,说明如何从国际标准化组织的储存中书写和阅读。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签