【发布时间】:2019-10-08 17:46:05
【问题描述】:
我正在使用 Prism 库来实现将 Image 与 BitmapImage 绑定。
这是xaml
<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 和设置ImageUri 和file.Path 都不起作用。 file.Path 为文件返回正确的值。
它仅在与网络文件一起使用时有效。
如果浏览本地磁盘文件,这是否与文件访问权限有关?
我认为这与 UWP 文件权限有关,但知道如何解决它吗?
【问题讨论】:
标签: data-binding uwp bitmap bitmapimage