使用&QUOT访问来自独立存储的图像在XAML; isostore:/"方案 [英] Accessing images from isolated storage in XAML using "isostore:/" scheme

查看:125
本文介绍了使用&QUOT访问来自独立存储的图像在XAML; isostore:/"方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网上下载图像,并将它们保存在独立存储,现在我要访问这些图像在我的XAML文件,给人一种开放的,以他们的参考。



我与IsoStoreSpy验证,看其妥善保存在那里我希望他们是,如果我打开该文件,字节流中读取我可以创建他们BitmapImages。但现在我想通过我的模型传递只是一个URI到IsolatedStorage位置,并让我的XAML加载图像优化我的图像处理。

 <图像HEIGHT =120WIDTH =120拉伸=统一的Horizo​​ntalAlignment =左> 
< Image.Source>
< BitmapImage的UriSource ={结合PodcastLogoUri}DecodePixelHeight =120DecodePixelWidth =120/>
< /Image.Source>
< /图像>

这是 PodcastLogoUri 乌里值,势必BitmapImage.UriSource:




isostore:/PodcastIcons/258393889fa6a0a0db7034c30a8d1c3322df55696137611554288265.jpg




下面是我是如何构建的:

 公众开放的PodcastLogoUri 
{
得到
{

乌里URI =新的URI(@isostore:/+ PodcastLogoLocation);
返回URI;
}
}



不过,我看不到在图像我UI。而且我相信影像在 PodcastLogoLocation



它应该是可能的参考图像,从用户界面独立存储像这样的Windows Phone 8?我究竟做错了什么?



修改如果我创建的BitmapImage直接使用相同的路径,并在XAML中使用的BitmapImage,它工作正常,我可以看到图像我希望看到有:

 <图像高度=120来源={结合PodcastLogo}WIDTH =120 拉伸=统一的Horizo​​ntalAlignment =左/> 



 公开的BitmapImage PodcastLogo 
{
得到
{
流流= NULL;
BitmapImage的LOGO =新的BitmapImage();
使用(VAR isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
如果(isoStore.FileExists(PodcastLogoLocation))
{
=流isoStore.OpenFile(PodcastLogoLocation, System.IO.FileMode.Open,FileAccess.Read);

{
logo.SetSource(流);
}
赶上(例外五)
{
}

}
}

返回标识;
}
}


解决方案

我想我正是这样做了,你正在试图做同样的事情。什么我发现是绝对的位置,独立存储商店使用 IsolatedStorageFile.GetUserStoreForApplication文件()。这有点像C:/数据/用户/ DefApps /应用程序数据/<应用产品ID> /本地/< YourFile.png>中;



我测试在Windows Phone 8这个解决方法,它为我的作品...



1。 XAML



 <图像宽度=40> 
< Image.Source>
< BitmapImage的DecodePixelWidth =40DecodePixelHeight =40UriSource ={绑定路径=图标}/>
< /Image.Source>
< /图像>



2。视图模型



 私人字符串_icon; 
公共字符串图标
{
得到
{
返回_icon;
}

{
如果(值= _icon!)
{
_icon =价值;
NotifyPropertyChanged(图标);
}
}
}



3。加载数据



  =文件名Myicon.png; 

IsolatedStorageFile店= IsolatedStorageFile.GetUserStoreForApplication();
如果(!store.FileExists(文件名))
{$ B $使用B(IsolatedStorageFileStream流=新IsolatedStorageFileStream(文件名,FileMode.Create,FileAccess.Write,存储))
流。写(imgBytes,0,imgBytes.Length);
}

//从清单获取产品ID。添加使用System.Linq的;如果您尚未
的Guid =的productId新的GUID((从System.Xml.Linq.XElement.Load(WMAppManifest.xml)清单。后代(应用程序),选择清单).SingleOrDefault()属性约(产品ID)值)。
串storeFile =C:/数据/用户/ DefApps /应用程序数据/+ productId.ToString(B)+/本地/+文件名;

this.Items.Add(新MyViewModel(){图标= storeFile});


I've downloaded images from the web and saved them to the Isolated Storage and now I want to access those images in my XAML file, giving a Uri as a reference to them.

I have verified with IsoStoreSpy that they are stored properly where I would expect them to be and I can create BitmapImages from them if I open the file and read in the byte stream. But now I want to optimize my image handling by passing just a Uri from my model to the IsolatedStorage location and letting my XAML load the image.

<Image Height="120" Width="120" Stretch="Uniform" HorizontalAlignment="Left">
   <Image.Source>
     <BitmapImage UriSource="{Binding PodcastLogoUri}" DecodePixelHeight="120" DecodePixelWidth="120" /> 
   </Image.Source>
</Image>

This is the PodcastLogoUri Uri value that is bound to BitmapImage.UriSource:

"isostore:/PodcastIcons/258393889fa6a0a0db7034c30a8d1c3322df55696137611554288265.jpg"

Here's how I've constructed it:

public Uri PodcastLogoUri
{
    get
    {

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

Still, I can't see an image in my UI. And I am sure the image is at PodcastLogoLocation.

Should it be possible to reference images to the UI from the isolated storage like this in Windows Phone 8? What am I doing wrong?

Edit: If I create the BitmapImage directly using the same path and use the BitmapImage in XAML, it works fine and I can see the image I expect to see there:

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

解决方案

I think I've done exactly the same thing that you're trying to do. What I've found is the absolute location where the Isolated Storage stores the file using IsolatedStorageFile.GetUserStoreForApplication(). This is something like "C:/Data/Users/DefApps/AppData/<App Product ID>/Local/<YourFile.png>";

I've tested this workaround on Windows Phone 8 and it works for me...

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

这篇关于使用&QUOT访问来自独立存储的图像在XAML; isostore:/&QUOT;方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆