【问题标题】:How to bind to the title of a UISegmentedControl segment with MVVMCross?如何使用 MVVMCross 绑定到 UISegmentedControl 段的标题?
【发布时间】:2015-07-23 09:04:54
【问题描述】:

是否可以将视图模型的属性绑定到 UISegmentedControl 中的段标题?

我知道 SetTitle() 方法,但不确定是否可以在 MvvmCross 中绑定到它。

【问题讨论】:

  • 这是我对类似问题的回答。只需将引用从 Android 更改为 Touch,VideoView 更改为 UISegmentedControl,并将属性名称从 VideoUri 更改为 Title:stackoverflow.com/questions/27132621/…
  • 谢谢!我将发布我自己的答案,但它完全基于您的其他答案以及相关更改。

标签: ios xamarin mvvmcross


【解决方案1】:

Kiliman's answer 基础上提出类似的问题。

按照该答案的前两个步骤进行操作。然后创建以下自定义绑定构建器。

public class MyTouchBindingBuilder : MvxTouchBindingBuilder
{
    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories (registry);

        registry.RegisterCustomBindingFactory<UISegmentedControl> ("Title", segmentTitle => new MvxSegmentTitleTargetBinding (segmentTitle));
    }
}

还有下面的自定义目标绑定。

public class MvxSegmentTitleTargetBinding : MvxConvertingTargetBinding
{
    public MvxSegmentTitleTargetBinding(object target) : base(target)
    {

    }

    public override Type TargetType
    {
        get {return typeof(MyViewModel);}
    }

    protected override void SetValueImpl(object target, object value)
    {
        var segmentControl = (UISegmentedControl)target;
        MyViewModel myViewModel = (MyViewModel)value;

        segmentControl.SetTitle(myViewModel.MyFirstValue, 0);
        segmentControl.SetTitle(myViewModel.MySecondValue, 1);
    }
}

然后像这样在你的视图中使用它。

set.Bind (MySegmentControl).For ("Title").To ((MyViewModel vm) => vm);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多