xamarin.formsmvvmcross

Cannot assign property "Converter": Property does not exist, or is not assignable, or mismatching type between value and property


I am writing an app with Xamarin.Forms and MvvmCross. I am having an issue with my converter. In my Core project:

public class BoolInverseValueConverter : MvxValueConverter<bool, bool>
{
    public bool Convert(bool value, Type targetType, CultureInfo culture, object parameter)
    {
        return !value;
    }
}

In my Forms project:

namespace MyApp.Forms.NativeConverters
{
    public class BoolInverseValueConverter : MvxNativeValueConverter<MyApp.Core.Converters.BoolInverseValueConverter>
    {
    }
}

In my xaml:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:MyApp.Forms.NativeConverters;assembly=MyApp.Forms"
x:Class="MyApp.Forms.App">
<Application.Resources>
    <converters:BoolInverseValueConverter x:Key="BoolInverseValueConverter" />
</Application.Resources>

In my page:

<views:MvxContentPage x:TypeArguments="viewModels:LoginViewModel"
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:mvx="clr-namespace:MvvmCross.Forms.Bindings;assembly=MvvmCross.Forms"
xmlns:viewModels="clr-namespace:MyApp.Core.ViewModels;assembly=MyApp.Core"
xmlns:localviews="clr-namespace:MyApp.Forms.Views;assembly=MyApp.Forms"
xmlns:resx="clr-namespace:MyApp.Core.Resources;assembly=MyApp.Core"
x:Class="MyApp.Forms.Pages.LoginPage"
Title="Login">
    <ContentPage.Content>
        <localviews:UserLoginView DataContext="{Binding .}" IsVisible="{mvx:MvxBind MyBoolVariable, Converter={StaticResource BoolInverseValueConverter}}"/>
    </ContentPage.Content>
</views:MvxContentPage>

When I run the UWP app I get the message:

Cannot assign property “Converter”: Property does not exist, or is not assignable, or mismatching type between value and property


Solution

  • I think MvvmCross is not scanning the assembly where you have your converter, thus it cannot be found. Try registering the converter class assembly in your Setup:

    protected override IEnumerable<Assembly> ValueConverterAssemblies
    {
        get
        {
            var toReturn = base.ValueConverterAssemblies.ToList();
            toReturn.Add(typeof(BoolInverseValueConverter).Assembly);
            return toReturn;
        }
    }
    

    HIH