绑定命令对转换器的行为

时间:2016-10-11 15:40:13

标签: c# listview xamarin.forms behavior ivalueconverter

我正在开发一个Xamarin.Forms项目。

我在listview上有一个行为,它正在使用转换器对命令进行绑定。我用XAML和C#做到了,它完美无缺。

XAML部分:

<ListView.Behaviors>
  <bh:ListViewPagingBehavior
    Command="{Binding LoadMoreLeadOfTheDateCommand}"
    Converter="{StaticResource ItemVisibilityConverter}">
  </bh:ListViewPagingBehavior>
</ListView.Behaviors>

但是现在我只需要在代码隐藏时执行此过程,因为我需要在代码隐藏中创建我的listview。

我试图像这样描述这个XAML:

ListViewPagingBehavior behavior = new ListViewPagingBehavior();
behavior.SetBinding(ListViewPagingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand", BindingMode.Default, new ItemVisibilityEventArgsConverter());
myListView.Behaviors.Add(behavior);

不幸的是,IValueConverter在Convert()方法中没有像以前那样检索相同的参数...

我的转换器:

public class ItemVisibilityEventArgsConverter : IValueConverter
{
    public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
    {
        var eventArgs = value as ItemVisibilityEventArgs;
        return eventArgs.Item;
    }

    public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

带有工作代码的Convert()的良好参数:

  • value = Xamarin.Forms.ItemVisibilityEventArgs

  • targetType = System.Object

  • parameter = null

  • culture = null

Convert()的错误参数与我的所有C#代码:

  • value = DelegateCommand

  • targetType = ICommand

  • parameter = null

  • culture = {fr-FR}

有人能告诉我在哪里错了吗? 非常感谢!

2 个答案:

答案 0 :(得分:0)

我不确定我是否已经很好地复制了你的情况,但这就是我想出来的。

behavior.SetBinding(ListViewPagingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand", BindingMode.Default, new ItemVisibilityEventArgsConverter());

在这里,您使用转换器转换您绑定的内容,这意味着您正在转换“LoadMoreLeadOfTheDateCommand”。这就是为什么你将DelegateCommand作为值。您的绑定应如下所示:

behavior.SetBinding(ListViewPagingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand");

然后只使用你的行为的ConverterProperty(或你称之为的任何东西):

behavior.Converter = new ItemVisibilityEventArgsConverter();

就像你在XAML中所做的那样。无论如何,这对我有用,我希望它也适合你:)

答案 1 :(得分:0)

我不知道[TestClass] public class BasicTests { private readonly IMapper _mapper; public BasicTests() { var config = new MapperConfiguration(cfg => { cfg.CreateMap<int?, int>() .ProjectUsing(src => src ?? default(int)); cfg.CreateMap<TestClassA, TestClassB>() .ForMember(dest => dest.IntProperty, opt => opt.MapFrom(src => src.NullableIntProperty)); cfg.CreateMap<TestClassA, TestClassA>() .ForMember(dest => dest.NullableIntProperty, opt => opt.MapFrom(src => src.NullableIntProperty)); cfg.CreateMap<TestClassB, TestClassA>() .ForMember(dest => dest.NullableIntProperty, opt => opt.MapFrom(src => src.IntProperty)); cfg.CreateMap<TestClassB, TestClassB>() .ForMember(dest => dest.IntProperty, opt => opt.MapFrom(src => src.IntProperty)); }); _mapper = new Mapper(config); } [TestMethod] public void CanMapNullableIntToInt() { var testQueryableSource = new List<TestClassA> { new TestClassA { NullableIntProperty = null } }.AsQueryable(); var testQueryableDestination = testQueryableSource.ProjectTo<TestClassB>(_mapper.ConfigurationProvider); } [TestMethod] public void CanMapNullableIntToNullableInt() { var testQueryableSource = new List<TestClassA> { new TestClassA { NullableIntProperty = null } }.AsQueryable(); var testQueryableDestination = testQueryableSource.ProjectTo<TestClassA>(_mapper.ConfigurationProvider); } [TestMethod] public void CanMapIntToNullableInt() { var testQueryableSource = new List<TestClassB> { new TestClassB { IntProperty = 0 } }.AsQueryable(); var testQueryableDestination = testQueryableSource.ProjectTo<TestClassA>(_mapper.ConfigurationProvider); } [TestMethod] public void CanMapIntToInt() { var testQueryableSource = new List<TestClassB> { new TestClassB { IntProperty = 0 } }.AsQueryable(); var testQueryableDestination = testQueryableSource.ProjectTo<TestClassB>(_mapper.ConfigurationProvider); } } 是什么,所以我只能猜测,但看看这句语法:

ListViewPagingBehavior

<ListView.Behaviors> <bh:ListViewPagingBehavior Command="{Binding LoadMoreLeadOfTheDateCommand}" Converter="{StaticResource ItemVisibilityConverter}"> </bh:ListViewPagingBehavior> </ListView.Behaviors> Converter的属性而不是ListViewPagingBehavior的属性(另一方面,转换器的可见性转换器看起来很奇怪)。

对此的等效C#看起来像这样:

{Binding}