Fluent Bindings 和 UIButton 标题 [英] Fluent Bindings and UIButton titles

查看:23
本文介绍了Fluent Bindings 和 UIButton 标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我的用户界面通常需要有本地化的字符串,所以我的视图模型提供了视图使用的所有字符串.这包括按钮上的标题等内容.

Since my user interfaces generally need to have localized strings, my view models provide all the strings which the views consume. This includes things like the titles on buttons.

在 iOS 端,按钮标题是通过 SetTitle 方法设置的.
为了让视图模型字符串 => 按钮标题映射起作用,MvvmCross 做了一些神奇的绑定转换来让它很好地工作.

on the iOS side, button titles are set via the SetTitle method.
In order to get a view model string => button title mapping to work, MvvmCross does some magic binding translation to get this to work nicely.

假设我的视图中有一个名为 Foo 的 UIButton,我想将其标题映射到我的视图模型中的 ButtonLabel 属性.为了建立这样的绑定,知道以下工作:

say I have a UIButton named Foo in my view and I want to map its title to a property ButtonLabel in my View Model. In know the following works in order to set up such a binding:

this.AddBindings(new Dictionary<object, string>() {
      {Foo, "Title ButtonTitle"}
 });

可以使用 MvvmCross 中的 Fluent Binding 系统设置相同的绑定吗?我一直在阅读 MvvmCross 源代码,但不太了解绑定代码.

Can this same binding be set up using the Fluent Binding system in MvvmCross? I've been reading through the MvvmCross source and I'm not quite getting the binding code.

以下不起作用(因为实际上按钮没有 Title 属性 - 它有一个 SetTitle 方法):

This following does NOT work (because in reality the button does not have a Title property - it has a SetTitle method):

 var set = this.CreateBindingSet<FooView, FooViewModel>();
 set.Bind(Foo).For(b => b.Title).To(vm => vm.ButtonTitle);
 set.Apply();

是否有另一种方法可以使用 Fluent Bindings 实现所需的结果?

Is there another way to achieve the desired result using Fluent Bindings?

推荐答案

因为按钮没有 Title 属性,那么

Because the button doesn't have a Title propery, then

set.Bind(Foo).For(b => b.Title).To(vm => vm.ButtonTitle);

不会编译.

但是,Xamarin.ios 的默认 MvvmCross 配置具有为 UIButton 和标题"定义的自定义绑定 - 请参阅:

However, the default MvvmCross configuraton for Xamarin.ios has a custom binding defined for UIButton and "Title" - see:

因此,您应该能够调用:

Because of this you should be able to call:

set.Bind(Foo).For("Title").To(vm => vm.ButtonTitle);

这应该设置与以下相同的绑定:

And this should setup the same binding as:

this.AddBindings(new Dictionary<object, string>() {
  {Foo, "Title ButtonTitle"}
});

有关自定义绑定的简要介绍,请参见:https://speakerdeck.com/crillous/custom-bindings-in-mvvmcross

For a very brief introduction into custom bindings see: https://speakerdeck.com/cirrious/custom-bindings-in-mvvmcross

这篇关于Fluent Bindings 和 UIButton 标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆