Capturing and updating an image source using MVVM in Xamarin

可紊 提交于 2021-01-29 09:50:55

问题


I'm trying to capture photo and display the captured image in Xamarin but changing the image source binding just doesn't seem to work. This seems really simple so I'm not quite sure where I'm going wrong.

MainPageViewModel.cs

public class MainPageViewModel : ViewModelBase
{

    private string _imageSource;
    public string ImageSource
    {
        get { return _imageSource; }
        set
        {
            _imageSource = value;
            SetProperty(ref _imageSource, value);
        }
    }

    public DelegateCommand TakePhotoCommand { get; private set; }

    public MainPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
        : base(navigationService)
    {
        Title = "Main Page";

        _dialogService = pageDialogService;

        TakePhotoCommand = new DelegateCommand(TakePhoto);

    }


    async void TakePhoto()
    {

        await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await _dialogService.DisplayAlertAsync("No Camera", ":( No camera avaialble.", "OK");

            return;
        }


        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
            Directory = "Sample",
            Name = "test.jpg"
        });

        if (file == null)
            return;

        // This does get called ok
        ImageSource = file.Path;

    }
}

ViewModelBase.cs

public class ViewModelBase : BindableBase, INavigationAware, IDestructible
{
    protected INavigationService NavigationService { get; private set; }

    private string _title;
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    public ViewModelBase(INavigationService navigationService)
    {
        NavigationService = navigationService;
    }

    public virtual void OnNavigatedFrom(NavigationParameters parameters)
    {

    }

    public virtual void OnNavigatedTo(NavigationParameters parameters)
    {

    }

    public virtual void OnNavigatingTo(NavigationParameters parameters)
    {

    }

    public virtual void Destroy()
    {

    }
}

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PhotoTesting.Views.MainPage"
             Title="{Binding Title}">

    <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
        <Image Source="{Binding ImageSource}" WidthRequest="200" HeightRequest="200" Aspect="AspectFill" />
        <Button x:Name="CameraButton" Text="Take Photo" Command="{Binding TakePhotoCommand}" />
    </StackLayout>

</ContentPage>

I know the image capture bit is working ok, the problem just seems to be setting the image.source after the page has loaded.


回答1:


You need to bind the Source property of Image to an ImageSource in MainPage.xaml
The ImageSource object can be obtained from the file stream. Here is the code:

public class MainPageViewModel : ViewModelBase
{
    private ImageSource _imageSource;
    public ImageSource ImageSource
    {
        get { return _imageSource; }
        set
        {
            _imageSource = value;
            SetProperty(ref _imageSource, value);
        }
    }

    public DelegateCommand TakePhotoCommand { get; private set; }

    public MainPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
        : base(navigationService)
    {
        Title = "Main Page";

        _dialogService = pageDialogService;

        TakePhotoCommand = new DelegateCommand(TakePhoto);

    }

    async void TakePhoto()
    {
        await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await _dialogService.DisplayAlertAsync("No Camera", ":( No camera avaialble.", "OK");

            return;
        }

        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
            Directory = "Sample",
            Name = "test.jpg"
        });

        if (file == null)
            return;

        // Here is the problem
        //ImageSource = file.Path;

        // This is the fix
        ImageSource = ImageSource.FromStream(() => file.GetStream());
    }
}


来源:https://stackoverflow.com/questions/52836842/capturing-and-updating-an-image-source-using-mvvm-in-xamarin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!