我已经长期使用MvvmLight,它完美地适合我的Windows和Windows Phone开发需求,但是我在处理新的Xamarin.iOS绑定功能时遇到了问题,这些功能是在版本5中引入的。
我查看了Flowers
示例,并尝试创建一个非常简单的绑定,但并没有像预期的那样工作:更新操作仅执行一次...
这里是视图控制器的代码:
public partial class MainViewController : UIViewController
{
private MainViewModel ViewModel { get; set; }
public MainViewController()
: base("MainViewController", null)
{
this.ViewModel = new MainViewModel();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.SetBinding(() => this.ViewModel.IsUpdated).WhenSourceChanges(() =>
{
this.updateLabel.Text = this.ViewModel.IsUpdated ? "It's okay !" : "Nope ...";
});
this.updateButton.SetCommand("TouchUpInside", this.ViewModel.UpdateCommand);
}
}
生成的部分类声明包含两个接口元素:
[Register ("MainViewController")]
partial class MainViewController
{
[Outlet]
MonoTouch.UIKit.UIButton updateButton { get; set; }
[Outlet]
MonoTouch.UIKit.UILabel updateLabel { get; set; }
void ReleaseDesignerOutlets ()
{
if (updateLabel != null) {
updateLabel.Dispose ();
updateLabel = null;
}
if (updateButton != null) {
updateButton.Dispose ();
updateButton = null;
}
}
}
相关的ViewModel如下:
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
this.UpdateCommand = new RelayCommand(() =>
{
this.IsUpdated = !this.IsUpdated;
});
}
private bool isUpdated;
public bool IsUpdated
{
get { return this.isUpdated; }
set
{
this.Set<bool>(ref this.isUpdated, value);
}
}
public RelayCommand UpdateCommand { get; private set; }
}
有人有一个可行的例子和一些解释吗?