我正在使用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应用时,我收到消息:
无法分配属性“转换器”:属性不存在或不存在 值和属性之间可分配或不匹配的类型
答案 0 :(得分:1)
不知道MvvmCross,但是在您的xaml文件中,
<Application.Resources>
和
<converters:BoolInverseValueConverter ...>
您添加了
<ResourceDictionary>
吗?
答案 1 :(得分:1)
我认为MvvmCross
没有在您要安装转换器的组件上进行扫描,因此找不到该组件。尝试在Setup
中注册转换器类程序集:
protected override IEnumerable<Assembly> ValueConverterAssemblies
{
get
{
var toReturn = base.ValueConverterAssemblies.ToList();
toReturn.Add(typeof(BoolInverseValueConverter).Assembly);
return toReturn;
}
}
HIH
答案 2 :(得分:0)
您的转换器应该如下所示
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;
}
答案 3 :(得分:0)
我最终制作了没有MvvmCross的转换器以使其正常工作。也许这是当前版本的MvvmCross(6.2.1)中的错误。