Accessing images from isolated storage in XAML using “isostore:/” scheme

为君一笑 提交于 2019-12-07 07:39:13

问题


I've downloaded images from the web and saved them to the Isolated Storage and now I want to access those images in my XAML file, giving a Uri as a reference to them.

I have verified with IsoStoreSpy that they are stored properly where I would expect them to be and I can create BitmapImages from them if I open the file and read in the byte stream. But now I want to optimize my image handling by passing just a Uri from my model to the IsolatedStorage location and letting my XAML load the image.

<Image Height="120" Width="120" Stretch="Uniform" HorizontalAlignment="Left">
   <Image.Source>
     <BitmapImage UriSource="{Binding PodcastLogoUri}" DecodePixelHeight="120" DecodePixelWidth="120" /> 
   </Image.Source>
</Image>

This is the PodcastLogoUri Uri value that is bound to BitmapImage.UriSource:

"isostore:/PodcastIcons/258393889fa6a0a0db7034c30a8d1c3322df55696137611554288265.jpg"

Here's how I've constructed it:

public Uri PodcastLogoUri
{
    get
    {

        Uri uri = new Uri(@"isostore:/" + PodcastLogoLocation);
        return uri;
    }
}

Still, I can't see an image in my UI. And I am sure the image is at PodcastLogoLocation.

Should it be possible to reference images to the UI from the isolated storage like this in Windows Phone 8? What am I doing wrong?

Edit: If I create the BitmapImage directly using the same path and use the BitmapImage in XAML, it works fine and I can see the image I expect to see there:

<Image Height="120" Source="{Binding PodcastLogo}"  Width="120" Stretch="Uniform" HorizontalAlignment="Left"/>
 public BitmapImage PodcastLogo
 {
     get
     {
          Stream stream = null;
          BitmapImage logo = new BitmapImage();
          using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
          {
              if (isoStore.FileExists(PodcastLogoLocation))
              {
                 stream = isoStore.OpenFile(PodcastLogoLocation, System.IO.FileMode.Open, FileAccess.Read);
                 try
                 {
                      logo.SetSource(stream);
                 }
                 catch (Exception e)
                 {
                 }

            }
        }

        return logo;
     }
 }

回答1:


I think I've done exactly the same thing that you're trying to do. What I've found is the absolute location where the Isolated Storage stores the file using IsolatedStorageFile.GetUserStoreForApplication(). This is something like "C:/Data/Users/DefApps/AppData/<App Product ID>/Local/<YourFile.png>";

I've tested this workaround on Windows Phone 8 and it works for me...

1. XAML

<Image Width="40">
    <Image.Source>
        <BitmapImage DecodePixelWidth="40" DecodePixelHeight="40" UriSource="{Binding Path=Icon}" />
    </Image.Source>
</Image>

2. ViewModel

private string _icon;
public string Icon
{
    get
    {
        return _icon;
    }
    set
    {
        if (value != _icon)
        {
            _icon = value;
            NotifyPropertyChanged("Icon");
        }
    }
}

3. Load data

filename = "Myicon.png";

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (!store.FileExists(filename))
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, store))
        stream.Write(imgBytes, 0, imgBytes.Length);
}

//get Product ID from manifest. Add using System.Linq; if you haven't already
Guid productId = new Guid((from manifest in System.Xml.Linq.XElement.Load("WMAppManifest.xml").Descendants("App") select manifest).SingleOrDefault().Attribute("ProductID").Value);
string storeFile = "C:/Data/Users/DefApps/AppData/" + productId.ToString("B") + "/Local/" + filename;

this.Items.Add(new MyViewModel() { Icon = storeFile });



回答2:


Sadly it seems this is not possible after all. I am a bit shocked and a lot disappointed by this. Can't really understand how MS doesn't support this case.

This is the answer I got over at MSDN forums:

It Will Not Support XAML Binding Directly From Isolated Storage With ISOStore URI Scheme.

Here is Detailed Answer For Your Answer.

http://mark.mymonster.nl/2013/05/24/yeah-windows-phone-supports-isolated-storage-access-through-an-uri-scheme-does-it

So that's it.



来源:https://stackoverflow.com/questions/18161143/accessing-images-from-isolated-storage-in-xaml-using-isostore-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!