首页 文章

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

提问于
浏览
4

我已经从网上下载了图像并将它们保存到了隔离存储中,现在我想在我的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?我究竟做错了什么?

Edit: 如果我使用相同的路径直接创建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 回答

  • 2

    我想我正在努力做到这一点 . 我发现的是隔离存储使用 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. 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 });
    
  • 5

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

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

    它不支持使用ISOStore URI方案直接从隔离存储绑定XAML . 这是您的答案的详细答案 . http://mark.mymonster.nl/2013/05/24/yeah-windows-phone-supports-isolated-storage-access-through-an-uri-scheme-does-it

    就是这样了 .

相关问题