无法分配 属性 "Converter":属性 不存在,或不可分配,或值与 属性 之间的类型不匹配

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

我正在用 Xamarin.Forms 和 MvvmCross 编写一个应用程序。我的转换器有问题。在我的 Core 项目中:

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

在我的 Forms 项目中:

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

在我的 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>

在我的页面中:

<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>

当我 运行 UWP 应用程序时,我收到消息:

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

不知道 MvvmCross,但在您的 xaml 文件中,在

之间
<Application.Resources>

<converters:BoolInverseValueConverter ...>

你添加了吗 <ResourceDictionary> ?

您的转换器应该如下所示

 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;

        }

我最终制作了没有 MvvmCross 的转换器以使其正常工作。也许这是当前版本的 MvvmCross (6.2.1) 的一个错误。

我认为 MvvmCross 没有扫描您安装转换器的程序集,因此找不到它。尝试在 Setup:

中注册转换器 class 程序集
protected override IEnumerable<Assembly> ValueConverterAssemblies
{
    get
    {
        var toReturn = base.ValueConverterAssemblies.ToList();
        toReturn.Add(typeof(BoolInverseValueConverter).Assembly);
        return toReturn;
    }
}