将图像源绑定到页面
Binding Image Source to Page
我正在尝试让图像出现在 UWP 应用程序的图像内部,以便我可以从图像选择器向最终用户显示所选图像。我可以根据用户通过文件选择器选择图像来正确设置当前文件,但表单上没有显示任何内容。有人可以帮忙吗?
MainPage.xaml
<Border Grid.Row="1" BorderBrush="WhiteSmoke" BorderThickness="2" Margin="5">
<Image Source="{Binding CurrentFile}" Stretch="Fill" />
</Border>
MainPageViewModel.cs
public StorageFile CurrentFile {
get
{
return _currentFile;
}
set
{
SetValue(ref _currentFile, value);
}
}
Here the problem is,The type StorageFile
cannot be converted as
Source for an image.So You have to use either a ValueConverter
or
change the type StorageFile
to Image
因此 属性 将如下所示:
private Image _CurrentFile;
public Image CurrentFile
{
get
{
return _CurrentFile;
}
}
其中 Image
在 System.Windows.Controls
命名空间下
您可以使用问题评论中提到的绑定转换器,或者将视图模型的类型 属性 更改为 ImageSource
:
public ImageSource CurrentImage
{
get { return currentImage; }
set { SetValue(ref currentImage, value); }
}
然后您将从 StorageFile
创建一个 BitmapImage
并将其分配给 属性,如下所示:
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
CurrentImage = bitmap;
正如您在问题中提到的 "image picker",您可以像这样使用上面的代码:
var picker = new FileOpenPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
viewModel.CurrentImage = bitmap;
}
Per @Clemens,我必须将 属性 的类型更改为图像源,然后在 UI 线程上异步加载它以显示图像。
在这种情况下,对于任何试图加载图像作为 WPF Image
类型源的人来说,答案是:
运行 通过 StorageFile 设置方法中的 UI Dispatcher:
//Acquire storagefile via a picker or something
public StorageFile CurrentFile
{
get { return currentImage; }
set {
SetValue(ref currentImage, value);
CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => SetCurrentImageAsync(value));
}
}
将图像异步加载到 CurrentImage 属性
public ImageSource CurrentImage
{
get { return currentImage; }
set { SetValue(ref currentImage, value); }
}
public async Task SetCurrentImageAsync(StorageFile file) {
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
this.CurrentImage = bitmap;
}
我正在尝试让图像出现在 UWP 应用程序的图像内部,以便我可以从图像选择器向最终用户显示所选图像。我可以根据用户通过文件选择器选择图像来正确设置当前文件,但表单上没有显示任何内容。有人可以帮忙吗?
MainPage.xaml
<Border Grid.Row="1" BorderBrush="WhiteSmoke" BorderThickness="2" Margin="5">
<Image Source="{Binding CurrentFile}" Stretch="Fill" />
</Border>
MainPageViewModel.cs
public StorageFile CurrentFile {
get
{
return _currentFile;
}
set
{
SetValue(ref _currentFile, value);
}
}
Here the problem is,The type
StorageFile
cannot be converted as Source for an image.So You have to use either aValueConverter
or change the typeStorageFile
toImage
因此 属性 将如下所示:
private Image _CurrentFile;
public Image CurrentFile
{
get
{
return _CurrentFile;
}
}
其中 Image
在 System.Windows.Controls
命名空间下
您可以使用问题评论中提到的绑定转换器,或者将视图模型的类型 属性 更改为 ImageSource
:
public ImageSource CurrentImage
{
get { return currentImage; }
set { SetValue(ref currentImage, value); }
}
然后您将从 StorageFile
创建一个 BitmapImage
并将其分配给 属性,如下所示:
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
CurrentImage = bitmap;
正如您在问题中提到的 "image picker",您可以像这样使用上面的代码:
var picker = new FileOpenPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
viewModel.CurrentImage = bitmap;
}
Per @Clemens,我必须将 属性 的类型更改为图像源,然后在 UI 线程上异步加载它以显示图像。
在这种情况下,对于任何试图加载图像作为 WPF Image
类型源的人来说,答案是:
运行 通过 StorageFile 设置方法中的 UI Dispatcher:
//Acquire storagefile via a picker or something
public StorageFile CurrentFile
{
get { return currentImage; }
set {
SetValue(ref currentImage, value);
CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => SetCurrentImageAsync(value));
}
}
将图像异步加载到 CurrentImage 属性
public ImageSource CurrentImage
{
get { return currentImage; }
set { SetValue(ref currentImage, value); }
}
public async Task SetCurrentImageAsync(StorageFile file) {
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
this.CurrentImage = bitmap;
}