Search code examples
xamarin.formsmvvmcross

mvvmcross xamarin.forms PictureChooser


I am trying to develop a photo app using the PictureChooser plugin. I see that the sample uses Xamarin.iOS. I've googled for examples where the plugin uses Xamarin.Forms but can't find any. I understand how binding works for labels, text editors, and buttons; however, the binding btw the page's image control and the viewmodel's byte[] has got me stomped.

DAA.UI project:

In CameraPage.XAML:

<Image x:Name="MyImage"
               Source="{Binding Bytes, Converter={StaticResource InMemoryImage}}"
               Aspect="Fill"
               HeightRequest="{OnPlatform iOS=300, Android=250}"
               WidthRequest="{OnPlatform iOS=300, Android=250}"
               HorizontalOptions="Center" />

In App.XAML:

<?xml version="1.0" encoding="utf-8" ?>
<Application
    x:Class="DamageAssessmentApp.UI.App"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="clr-namespace:MvvmCross.Forms;assembly=MvvmCross.Forms"
    xmlns:resources="clr-namespace:DAA.UI.Resources"
    xmlns:local="using:DAA.UI"
    xmlns:nativeValueConverters="using:DAA.UI.NativeValueConverters">
    <Application.Resources>

        <!--  Application resource dictionary  -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <resources:Colors />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
        <nativeValueConverters:NativeInMemoryImageValueConverter x:Key="InMemoryImage"/>
    </Application.Resources>
</Application>

Value Converter file:

using MvvmCross.Forms.Converters;
namespace DAA.UI.NativeValueConverters
{
    public class NativeInMemoryImageValueConverter : MvxNativeValueConverter<MvxInMemoryImageValueConverter>
    {
    }
}

The compiler can't find MvxInMemoryImageValueConverter in the value converter file.


Solution

  • If you are using MVVMCross you should find an example that works with Xamarin.Forms, in which case a good place to start it's their Github.

    Or you have to implement it in each platform and use a DependencyService to get the implementation

    Other Alternatives

    1. Xamarin Community Toolkit

    Another alternative for a camera App is Xamarin Community Toolkit Camera View. In that same link there is an example. But there are more examples in their Github. This is fully compatible with Xamarin.Forms and brings a little more control over the CameraView

    1. Xamarin.Essentials

    Xamarin.Essentials offers the MediaPicker that let's the user upload a photo from the gallery or take a new one. But the action of the photo in handled by the OS, so for you it's like a black box. You call the function, and get the photo.