【问题标题】:UWP Set BitmapImage for Image ControlUWP为图像控件设置BitmapImage
【发布时间】: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


    【解决方案1】:

    设置ImageResult 的方式适用于 UWP 平台。但是使用file.Path 设置图像源将不起作用。 UWP 在沙盒中运行,我们无法使用 xaml 中的路径访问文件。我检查了你的代码,它看起来不像 uwp 项目。因为 UWP 不包含 Label 控件。如果项目是xamarin,请参考xamarin image文档

    更新

    Froms 项目是共享库,您不应在其中调用 UWP FileOpenPicker。根据您的要求,您可以为 Xamarin 创建依赖服务并将位图转换为 Xamarin 图像源,更多请参考case

    【讨论】:

    • 是的,它是 xamarin.uwp。我想在我的表单中显示用户使用文件选择器选择的本地图像。有什么办法可以实现吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2013-01-24
    • 2021-03-29
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多