代码之家  ›  专栏  ›  技术社区  ›  Drake

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

  •  0
  • Drake  · 技术社区  · 6 年前

    我正在用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应用程序时,我得到一条消息:

    无法分配属性转换器:属性不存在或不存在 值和属性之间的可赋值或不匹配类型

    4 回复  |  直到 6 年前
        1
  •  1
  •   fmaccaroni    6 年前

    我想 MvvmCross Setup :

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

        2
  •  1
  •   Jason    6 年前

    不知道MvvmCross,但在xaml文件中

    <Application.Resources>
    

    <converters:BoolInverseValueConverter ...>
    

    你加了吗 <ResourceDictionary> ?

        3
  •  0
  •   Venkat    6 年前

    你的转换器应该如下所示

     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;
    
            }
    
        4
  •  0
  •   Drake    6 年前