【发布时间】:2015-03-24 22:50:25
【问题描述】:
我已经使用 MvvmLight 很长时间了,它非常适合我的 Windows 和 Windows Phone 开发需求,但我在版本 5 中引入的新 Xamarin.iOS 绑定功能遇到了困难。
我检查了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; }
}
谁有一个可行的例子,以及一些解释?
【问题讨论】:
标签: c# ios mvvm xamarin mvvm-light