2013-02-14 57 views
3

我想显示通过结合存储在StorageFile图片内容的图片,但不管我试图做到这一点似乎并没有工作。显示存储在存储文件中metroapp

这里有两种解决方案我已经测试:

string img =(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))).Path; 

然后通过结合返回到源物业所获得的路径(一个完整的绝对路径文件),并且:

BitmapImage img = await LoadImage(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))); 

    private static async Task<BitmapImage> LoadImage(StorageFile file) 
     { 
      BitmapImage bitmapImage = new BitmapImage(); 
      FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read); 

      bitmapImage.SetSource(stream); 


      return bitmapImage; 

     } 

稍后将最终的bitmapImage返回到绑定属性。

这些方法没有工作..

千万人有一个想法?

编辑:FIX

下面是解决该问题的代码:

BitmapImage img = new BitmapImage() { UriSource = new Uri(LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) }; 

我做了2个样品的混合上面:我创造了我的BitmapImage从图片的绝对URI (LOCAL_REPOSITORY包含对本地存储的引用:ApplicationData.Current.LocalFolder

我仍然无法确定为什么其他两种方式失败:我通常将图像直接绑定到Uri,字符串或BitmapImage的,和它的作品..

+0

你什么错误? – 2013-02-14 21:25:06

+0

我没有收到任何信息:图片没有被显示,没有发生崩溃,也没有提出错误。 我解决了这个问题(请参阅编辑),但我仍然无法确定为什么其他两个样本不起作用。 – 2013-02-14 21:33:26

+0

图片是否位于其他样本的本地文件夹中?商店应用程序没有对文件系统的任意访问权限。 – 2013-02-14 21:40:11

回答

8

下面的代码演示了如何使用的LoadImage方法中的应用:创建一个空白的应用程序,添加图像和按钮的主网页。

private async void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     // load file from document library. Note: select document library in capabilities and declare .png file type 
     string filename = "Logo.png"; 
     Windows.Storage.StorageFile sampleFile = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(filename); 
     // load file from a local folder 
     //Windows.Storage.StorageFile sampleFile = sampleFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\\Logo.png"); 

     BitmapImage img = new BitmapImage(); 
     img = await LoadImage(sampleFile); 
     myImage.Source = img; 
    } 

    private static async Task<BitmapImage> LoadImage(StorageFile file) 
    { 
     BitmapImage bitmapImage = new BitmapImage(); 
     FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read); 

     bitmapImage.SetSource(stream); 

     return bitmapImage; 

    } 
+1

加空指针异常的检查(例如:file.OpenAsync中的LoadImage) – 2015-09-16 09:36:40

+0

你好的人,一个问题...是更多钞票base64转换字符串StorageFile?我想附加一个base64生成的文件...请参阅当前的代码http://screencast.com/t/N0Sr2G3nJ – jdnichollsc 2016-01-08 22:10:15