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

时间:2013-08-10 10:50:26

标签: c# xaml windows-phone-8 isolatedstorage

我已从网上下载图像并将其保存到隔离存储中,现在我想在我的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>

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

  

“isostore:/PodcastIcons/258393889fa6a0a0db7034c30a8d1c3322df55696137611554288265.jpg”

以下是我构建它的方式:

public Uri PodcastLogoUri
{
    get
    {

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

但是,我在UI中看不到图像。我相信图片位于PodcastLogoLocation

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

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

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

2 个答案:

答案 0 :(得分:5)

我认为我完成了与您尝试做的完全相同的事情。我发现的是隔离存储使用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>

<强> 2。视图模型

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

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

答案 1 :(得分:2)

可悲的是,似乎这毕竟不可能。我对此感到有些震惊和失望。无法真正了解MS如何不支持这种情况。

这是我在MSDN论坛上得到的答案:

  

它不支持XAML直接从隔离存储绑定   ISOStore URI Scheme。

     

以下是答案的详细解答。

     

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

就是这样。