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

4
我从网上下载了图片并将其保存到独立存储中,现在我想通过给出Uri来访问我的XAML文件中的这些图像。
我已使用IsoStoreSpy验证它们是否被正确存储在预期位置,并且如果我打开文件并读取字节流,我可以从它们创建BitmapImages。但现在,我想通过从我的模型传递仅Uri到独立存储位置并让我的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;
    }
}

然而,我在用户界面中看不到图像。我确信该图像位于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;
     }
 }

尝试不使用 UriKind.Absolute 吗? - Neil Turner
谢谢您的建议,但是它没有起到帮助作用。 :-( - Johan Paul
也许我的问题并不像我预期的那样直截了当。我还在MSDN论坛上发布了它,但是没有得到答案。我真的以为只是我没有理解某些东西,很快就会有人指出来。但事实并非如此。http://social.msdn.microsoft.com/Forums/wpapps/en-US/0c1948a7-d4fc-4aa3-b30d-97213a7a816f/accessing-images-from-isolated-storage-in-xaml-using-isostore-scheme#0c1948a7-d4fc-4aa3-b30d-97213a7a816f - Johan Paul
2个回答

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. ViewModel

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

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接