Tho*_*tin 3 c# microsoft-metro windows-8 .net-4.5
我想通过绑定显示存储在StorageFile中的图片的内容,但无论我想要做什么它似乎都不起作用.
以下是我已经测试过的两个解决方案:
string img =( await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))).Path;
Run Code Online (Sandbox Code Playgroud)
然后通过绑定将获得的路径(文件的完整绝对路径)返回到源Property,并且:
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;
}
Run Code Online (Sandbox Code Playgroud)
稍后将最终的bitmapImage返回到我的binded属性.
这些方法都不起作用..
有人有想法吗?
编辑:修复
这是解决问题的代码:
BitmapImage img = new BitmapImage() { UriSource = new Uri( LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) };
Run Code Online (Sandbox Code Playgroud)
我做了2个样品的混合上面:我创造了我的BitmapImage从图片的绝对URI(LOCAL_REPOSITORY包含本地存储的参考:ApplicationData.Current.LocalFolder)
我仍然无法理解为什么其他两种方法失败:我通常将我的图像直接绑定到Uri,字符串或BitmapImage,它可以工作..
下面的代码显示了如何在app中使用loadimage方法:创建一个空白应用程序,将一个Image和一个Button添加到主页面.
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11720 次 |
| 最近记录: |