【发布时间】:2014-03-04 21:09:43
【问题描述】:
使用 c#、VS 2013、Windows Store App
我有一些 Json 文件,其中包含有关某些事件的信息,在此文件中,用户可以存储一些数据,以及其中一个数据 - 事件图像的路径,例如:
{"Events":
[
{
"UniqueId": "Day-1-Item-1",
"Name": "Item Title: 1",
"Place": "Item Subtitle: 1",
"Description": "Event 1",
"Start" : "2,25,2014,2,14",
"End" : "2,25,2014,16,44",
"ImagePath" : "Assets/appbar.calendar.png"
},
{
"UniqueId": "Day-1-Item-2",
"Name": "Item Title: 1",
"Place": "Item Subtitle: 1",
"Description": "Event 1",
"Start" : "2,25,2014,2,14",
"End" : "2,25,2014,16,44",
"ImagePath" : "D:\\Pictures\\Abstract\\8.jpg"
}
]
}
所以我将所有这些信息绑定到 UI(使用 XAML)并得到如下图所示的内容:
因此,对于我将图片从 Assets 文件夹中放入项目中的那些项目(意思是 "ImagePath" : "Assets/appbar.calendar.png"),我得到了正确的结果(在图片上是日历),但对于事件,我从硬盘驱动器中选择图片 - 我什么也没得到。
问题:是否可以将 UI 绑定到硬盘中的数据?或者我需要在 App 的 assets 文件夹(或LocalState 文件夹)中创建一些图片副本?
编辑
所以现在我有下一个:
public object Convert(object value, Type targetType, object parameter, string language)
{
string filePath = value.ToString(); //get path to picture
BitmapImage image = new BitmapImage(); //create bitmap image blank
if (filePath.Contains("Assets")) //if standart picture
{
return LoadImageFromAssetsLibrary(image);
}
else
{
return LoadImageFromString(image, filePath);
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
private async Task<ImageSource> LoadImageFromString(BitmapImage image, string path)
{
StorageFolder folder = KnownFolders.PicturesLibrary; //get folder
StorageFile file = await folder.GetFileAsync(path); //find and read file
//set source of file to image
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
return ...// i dont know how to return ImageSource from image that i create in code above
}
所以我有点堆栈 - 如何从图像返回 ImageSource,尝试 (ImageSource)image 但得到(targetType - ImageSource):
编辑 2
经过小的更改(来自上次更新),可以使用 Converter 显示来自应用文件夹的图片,但来自图片库 - 不 - 认为问题在于访问...
改变了:
public object Convert(object value, Type targetType, object parameter, string language)
{
string filePath = value.ToString(); //get path to picture
BitmapImage image = new BitmapImage(); //create bitmap image blank
if (filePath.Contains("Assets")) //if standart picture
{
LoadImageFromAssetsLibrary(image, filePath);
return image;
}
else
{
LoadImageFromString(image, filePath);
return image;
}
}
//for this method required access
private async Task LoadImageFromString(BitmapImage image, string path)
{
StorageFolder folder = KnownFolders.PicturesLibrary; //get folder
StorageFile file = await folder.GetFileAsync(path); //find and read file
//set source of file to image
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
}
//this work correctly
private async Task LoadImageFromAssetsLibrary(BitmapImage image, string path)
{
StringBuilder builder = new StringBuilder();
builder.Append("ms-appx:///");
builder.Append(path);
string localPath = builder.ToString();
Uri requestedFileUri = new Uri(localPath); //set default folder
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(requestedFileUri); //read file
//set source of file to image
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
}
另外,我允许访问 pic 文件夹:
EDIT3
【问题讨论】:
-
对于像这样的简单图像,放弃 pic/image 文件并将其转换为 xaml 作为模板并将其放入您的资源中。
-
它只是一个示例,图像可以是用户可以选择的任何人,例如选择(未显示)它的照片
-
我认为您应该复制用户选择的图像。也许您可以使用默认的图像文件夹,可能会有所帮助。
标签: c# .net xaml data-binding windows-store-apps