ios - 如何使用 MVVMCross 绑定(bind)到 UISegmentedControl 段的标题?
<p><p>是否可以将 View 模型的属性绑定(bind)到 UISegmentedControl 中的段标题?</p>
<p>我知道 SetTitle() 方法,但不确定是否可以在 MvvmCross 中绑定(bind)到它。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>从 <a href="https://stackoverflow.com/questions/27132621/mvvmcross-videoview-url-binding/27137091#27137091" rel="noreferrer noopener nofollow">Kiliman's answer</a> 开始类似的问题。</p>
<p>按照该答案的前两个步骤进行操作。然后创建以下自定义绑定(bind)构建器。</p>
<pre><code>public class MyTouchBindingBuilder : MvxTouchBindingBuilder
{
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories (registry);
registry.RegisterCustomBindingFactory<UISegmentedControl> ("Title", segmentTitle => new MvxSegmentTitleTargetBinding (segmentTitle));
}
}
</code></pre>
<p>还有下面的自定义目标绑定(bind)。</p>
<pre><code>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);
}
}
</code></pre>
<p>然后像这样在你的 View 中使用它。</p>
<pre><code>set.Bind (MySegmentControl).For ("Title").To ((MyViewModel vm) => vm);
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使用 MVVMCross 绑定(bind)到 UISegmentedControl 段的标题?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31582823/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31582823/
</a>
</p>
页:
[1]