Drake
Drake

Reputation: 2703

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

Upvotes: 1

Views: 1165

Answers (5)

Ted Kerley
Ted Kerley

Reputation: 11

For anyone else encountering this error, there is another possible reason which had me stumped for a while.

I had accidentally included the following reference (in a maui app)

using System.Windows.Data;

which meant that the IValueConverter could not be assigned to a converter from the Microsoft.Maui.Controls namespace.

Removing the using statement fixed the problem.

Upvotes: 0

fmaccaroni
fmaccaroni

Reputation: 3916

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

Upvotes: 1

Drake
Drake

Reputation: 2703

I ended up making the converter without MvvmCross to have it working. Maybe it's a bug in the current version of MvvmCross (6.2.1).

Upvotes: 0

Venkat
Venkat

Reputation: 140

Your converter should be something like below

 public class BoolInverseValueConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null || (value as bool?) == false)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value == null || (value as bool?) == false)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;

        }

Upvotes: 0

Yang You
Yang You

Reputation: 2668

no idea about MvvmCross, but in your xaml file, between

<Application.Resources>

and

<converters:BoolInverseValueConverter ...>

did you add <ResourceDictionary> ?

Upvotes: 1

Related Questions