UWP显示来自外部路径的图像?
我正在尝试将一个简单的WPF程序移植到UWP。它是一个将一些自定义图像分析例程应用于一组图像的工具 基本功能:UWP显示来自外部路径的图像?,uwp,Uwp,我正在尝试将一个简单的WPF程序移植到UWP。它是一个将一些自定义图像分析例程应用于一组图像的工具 基本功能: 将图像添加到库视图(可能是100000个图像) 运行图像分析 导出数据 我遇到的问题是在图库中显示图像 在WPF中,我可以将一个列表框绑定到输入图像的可观察集合,就像 UWP中的等价物是什么 我尝试过上面的方法(减去IsAsync和Freeze部分),但是图像的宽度和高度为0 我想也许我必须从路径创建一个StorageFile,打开它并设置位图源代码,但是我不能在属性getter中使
列表框
绑定到输入图像的可观察集合
,就像
UWP中的等价物是什么
- 我尝试过上面的方法(减去
IsAsync
和Freeze
部分),但是图像的宽度和高度为0李>
- 我想也许我必须从路径创建一个
StorageFile
,打开它并设置位图源代码,但是我不能在属性getter中使用async
方法
有什么解决办法吗
注意:我在appxmanifest中启用了broadFileSystemAccess,并在“设置->隐私->文件系统”中为该应用打开了该选项,以下是我学到的:
即使启用了broadFileSystemAccess
,外部文件似乎也必须通过StorageFile
访问
StorageFile file = await StorageFile.GetFileFromPathAsync(@"C:\path\to\file");
您可以实例化BitmapImage
属性,并在首次加载图像列表时直接绑定到该属性,例如
BitmapImage image = new BitmapImage();
var storageFile = await StorageFile.GetFileFromPathAsync(path);
using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
InputImage.Source = image;
对于单个图像来说,这很好,但是对于1000个图像来说,存在一个问题-每个图像都会被加载,占用大量的时间和内存,即使使用GridView和其他控件的虚拟化方面也是如此
解决方案是使用异步绑定(是的,似乎这实际上是可能的),如上所述
程序:
安装Nito.AsyncEx
NuGet软件包
对属性使用以下命令:
public INotifyTaskCompletion<BitmapImage> ImageAsync
{
get { return NotifyTaskCompletion.Create(GetImageAsync()); }
}
public async Task<BitmapImage> GetImageAsync()
{
BitmapImage image = new BitmapImage();
var storageFile = await StorageFile.GetFileFromPathAsync(Path);
using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
return image;
}
解决了,以下是我学到的:
即使启用了broadFileSystemAccess
,外部文件似乎也必须通过StorageFile
访问
StorageFile file = await StorageFile.GetFileFromPathAsync(@"C:\path\to\file");
您可以实例化BitmapImage
属性,并在首次加载图像列表时直接绑定到该属性,例如
BitmapImage image = new BitmapImage();
var storageFile = await StorageFile.GetFileFromPathAsync(path);
using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
InputImage.Source = image;
对于单个图像来说,这很好,但是对于1000个图像来说,存在一个问题-每个图像都会被加载,占用大量的时间和内存,即使使用GridView和其他控件的虚拟化方面也是如此
解决方案是使用异步绑定(是的,似乎这实际上是可能的),如上所述
程序:
安装Nito.AsyncEx
NuGet软件包
对属性使用以下命令:
public INotifyTaskCompletion<BitmapImage> ImageAsync
{
get { return NotifyTaskCompletion.Create(GetImageAsync()); }
}
public async Task<BitmapImage> GetImageAsync()
{
BitmapImage image = new BitmapImage();
var storageFile = await StorageFile.GetFileFromPathAsync(Path);
using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
return image;
}
您好,您可以将listview中的in-Image的源绑定到包含uri的字符串。无需创建BitmapImagesource@thezapper这是我第一次使用文件://
样式的URI,但它在UWP中不起作用,只是一个空白图像。(我实际上使用的是GridView,但这可能无关紧要)。如果URI指向程序集中的某个对象(例如资产/…)它可以工作,但不能在外部。可能您的图像大小是zip。@lindexi解决了它-请参阅我的答案。您好,您可以将listview中的in-Image的源绑定到包含uri的字符串。无需创建BitmapImagesource@thezapper这是我第一次使用文件://
风格的URI,但它在UWP中不起作用,只是一个空白图像。(我实际上使用的是GridView,但这可能无关紧要)它可以工作,但不在外部。可能您的图像大小是zip。@lindexi解决了这个问题-请参阅我的答案。但是您的图像不能在路径更改时更新。@lindexi这对我来说不是问题,但我想如果路径更改,您可以从路径设置程序为ImageAsync触发属性更改事件。但是,当路径更改时,您的图像无法更新。@lindexi t这对我来说不是问题,但我想如果路径发生了更改,您可以从路径设置程序为ImageAsync触发属性更改事件。