【问题标题】:Cannot assign property "Converter": Property does not exist, or is not assignable, or mismatching type between value and property无法分配属性“转换器”:属性不存在,或不可分配,或值和属性之间的类型不匹配
【发布时间】:2019-03-28 06:15:52
【问题描述】:

我正在使用 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 应用时,我收到以下消息:

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

【问题讨论】:

    标签: xamarin.forms mvvmcross


    【解决方案1】:

    我认为MvvmCross 没有扫描您拥有转换器的程序集,因此无法找到它。尝试在 Setup 中注册转换器类程序集:

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

    【讨论】:

      【解决方案2】:

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

      <Application.Resources>
      

      <converters:BoolInverseValueConverter ...>
      

      你添加了吗 &lt;ResourceDictionary&gt;?

      【讨论】:

      • 没有,但似乎不需要。
      【解决方案3】:

      您的转换器应该如下所示

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

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

        【讨论】:

          猜你喜欢
          • 2018-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-29
          • 2017-07-11
          • 2019-12-07
          相关资源
          最近更新 更多