0

我有一个图像视图,我在设置源时遇到问题。我正在使用一个按钮来执行一个 TakePictureCommand,它调用 TakePicture() 方法(如下所示),该方法反过来设置我的源“ImageSource”。调试该方法显示图像正在进入,但我从未在 UI 中看到它出现。

我可能没有正确设置图像的绑定,这就是我所拥有的:

Image avatar = new Image();
avatar.Source = ImageSource;

Button setImageBtn = new Button{ Text = "Photo" };
setImageBtn.Clicked += async (sender, e) =>
{
    string action = await DisplayActionSheet(
           "Event Photo", "Cancel", null, OPTION_CAMERA, OPTION_GALLERY);

    if(action == OPTION_CAMERA) {
        TakePictureCommand.Execute(null);
    }

    else if(action == OPTION_GALLERY) {
        SelectPictureCommand.Execute(null);
    }
};

拍照片()

private async Task<MediaFile> TakePicture()
{
    Setup();

    ImageSource = null;

    return await _mediaPicker.TakePhotoAsync(
        new CameraMediaStorageOptions { 
            DefaultCamera = CameraDevice.Front, 
            MaxPixelDimension = 400 
    }).ContinueWith(t =>
    {
        if (t.IsFaulted)
        {
            Status = t.Exception.InnerException.ToString();
        }
        else if (t.IsCanceled)
        {
            Status = "Canceled";
        }
        else
        {
            var mediaFile = t.Result;

            ImageSource = ImageSource.FromStream(() => mediaFile.Source);

            return mediaFile;
        }

        return null;
    }, _scheduler);
}

我在这里想念什么?

4

1 回答 1

0

以下方法对我有用:

首先,在 XAML 中我添加了图像视图

<Image Source="{Binding ImageSource}" VerticalOptions="Fill" HorizontalOptions="Fill"
                    Aspect="AspectFit"/>

其次,在 ViewModel 中,我添加了 ImageSource 属性,如下所示:

public ImageSource ImageSource
{
    get { return _imageSource; }
    set { this.SetProperty(ref _imageSource, value); }
}

第三,在命令处理程序中:

await TakePicture ();

第四,TakePicture() 方法的代码和你写的一样。我的设置():

if (_mediaPicker != null)
            return;
var device = Resolver.Resolve<IDevice>();
_mediaPicker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
于 2015-03-20T21:12:36.123 回答