使用"isostore:/"方案从XAML中的隔离存储访问映像

Joh*_*aul 4 c# xaml isolatedstorage windows-phone-8

我已经从网上下载了图像并将它们保存到了隔离存储中,现在我想在我的XAML文件中访问这些图像,并将Uri作为对它们的引用.

我已经使用IsoStoreSpy验证了它们存储在我期望它们的正确位置,如果我打开文件并读入字节流,我可以从它们创建BitmapImages.但是现在我想通过将Uri从我的模型传递到IsolatedStorage位置并让我的XAML加载图像来优化我的图像处理.

<Image Height="120" Width="120" Stretch="Uniform" HorizontalAlignment="Left">
   <Image.Source>
     <BitmapImage UriSource="{Binding PodcastLogoUri}" DecodePixelHeight="120" DecodePixelWidth="120" /> 
   </Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)

这是PodcastLogoUri绑定到BitmapImage.UriSource 的Uri值:

"isostore:/PodcastIcons/258393889fa6a0a0db7034c30a8d1c3322df55696137611554288265.jpg"

这是我构建它的方式:

public Uri PodcastLogoUri
{
    get
    {

        Uri uri = new Uri(@"isostore:/" + PodcastLogoLocation);
        return uri;
    }
}
Run Code Online (Sandbox Code Playgroud)

不过,我在UI中看不到图像.我相信图像是在PodcastLogoLocation.

是否可以从Windows Phone 8中的隔离存储中将图像引用到UI?我究竟做错了什么?

编辑:如果我使用相同的路径直接创建BitmapImage并使用XAML中的BitmapImage,它工作正常,我可以看到我期望在那里看到的图像:

<Image Height="120" Source="{Binding PodcastLogo}"  Width="120" Stretch="Uniform" HorizontalAlignment="Left"/>
Run Code Online (Sandbox Code Playgroud)
 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;
     }
 }
Run Code Online (Sandbox Code Playgroud)

Kap*_*apé 5

我想我完成了与你想要做的完全相同的事情.我发现的是Isolated Storage存储文件的绝对位置IsolatedStorageFile.GetUserStoreForApplication().这就像是"C:/Data/Users/DefApps/AppData/<App Product ID>/Local/<YourFile.png>";

我在Windows Phone 8上测试了这个解决方法,它对我有用......

1. XAML

<Image Width="40">
    <Image.Source>
        <BitmapImage DecodePixelWidth="40" DecodePixelHeight="40" UriSource="{Binding Path=Icon}" />
    </Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)

2. ViewModel

private string _icon;
public string Icon
{
    get
    {
        return _icon;
    }
    set
    {
        if (value != _icon)
        {
            _icon = value;
            NotifyPropertyChanged("Icon");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

3.加载数据

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 });
Run Code Online (Sandbox Code Playgroud)