data-binding - UWP为图像控件设置BitmapImage
问题描述
我正在使用 Prism 库来实现与 BitmapImage 的绑定图像。
这是 xml
<Button Grid.Row="1" Text="Resize Img" Command="{Binding ImageResize}"></Button>
<Image Grid.Row="2" Source="{Binding ImageResult}"></Image>
<Image Grid.Row="3" Source="{Binding ImageUri}"></Image>
<Label Text="{Binding ImageUri}"></Label>
这是 ImagePageViewModel
public class ImagePageViewModel : BindableBase
{
public ImagePageViewModel()
{
}
private string _imageUri;
public string ImageUri
{
get { return _imageUri; }
set { SetProperty(ref _imageUri, value); }
}
private BitmapImage _imageResult;
public BitmapImage ImageResult
{
get { return _imageResult; }
set { SetProperty(ref _imageResult, value); }
}
private DelegateCommand _imageSelect;
public DelegateCommand ImageSelect =>
_imageSelect ?? (_imageSelect = new DelegateCommand(ExecuteImageSelect));
async void ExecuteImageSelect()
{
try
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// Set the image source to the selected bitmap
BitmapImage bitmapImage = new BitmapImage();
//bitmapImage.DecodePixelWidth = 600; //match the target Image.Width, not shown
await bitmapImage.SetSourceAsync(fileStream);
ImageResult = bitmapImage;
ImageUri = file.Path;
//ImageUri = @"http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg";
}
}
else
{
ImageUri = "";
}
}
catch (Exception ex)
{
throw ex;
}
}
}
对于上面的代码,设置ImageResult
和设置 ImageUrifile.Path
两者都不起作用。file.Path 返回文件的正确值。
它仅在与网络文件一起使用时有效。
如果您浏览本地磁盘文件,这与文件访问权限有关吗?
我认为这与 UWP 文件权限有关,但知道如何解决吗?
解决方案
推荐阅读
- laravel - 使用 \Mail::send 不能设置标题
- android - 如何通过单击按钮重试方法?
- yarnpkg - yarnpkg 是 node.js 包管理器 yarn 的别名吗?
- python - Lifetimes 包给出不一致的结果
- ios - 如何检查当前 viewController 或根 viewController?
- objective-c - Tableviewcell如何为项目组提供备用单元格颜色
- google-sheets - Google 表格中财务数据的自定义聚合
- postman - 是否可以在本地使用预请求创建邮递员环境范围?
- ruby-on-rails - 在 Rails 中以不同语言保存输入字段
- python - Python 在布尔表达式中使用多个运算符