使用“isostore:/”方案从 XAML 中的独立存储访问图像

2024-05-23

我已经从网上下载了图像并将它们保存到独立存储中,现在我想在我的 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>

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

“isostore:/PodcastIcons/258393889fa6a0a0db7034c30a8d1c3322df55696137611554288265.jpg”

这是我的构建方式:

public Uri PodcastLogoUri
{
    get
    {

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

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

我想我已经做了和你想做的一模一样的事情。我发现的是独立存储使用以下方式存储文件的绝对位置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 });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用“isostore:/”方案从 XAML 中的独立存储访问图像 的相关文章

随机推荐