[英]Binding a property to a ViewModel with MvvmLight and Xamarin.iOS
I've used MvvmLight for a long time now which fits perfectly my needs for my Windows and Windows Phone developments, but I struggle with the new Xamarin.iOS binding features introduced in version 5. 我已经使用了MvvmLight很长一段时间,这完全符合我对Windows和Windows Phone开发的需求,但我很难使用版本5中引入的新Xamarin.iOS绑定功能。
I've checked the Flowers
sample, and tried to create a really simple binding that doesn't work as expected : the update action is executed only once ... 我检查了
Flowers
示例,并尝试创建一个非常简单的绑定,它不能按预期工作:更新操作只执行一次...
Here the code of the view controller : 这里是视图控制器的代码:
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);
}
}
The generated partial class declaration with two interface elements: 生成的具有两个接口元素的部分类声明:
[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;
}
}
}
And the associated ViewModel : 以及相关的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; }
}
Anybody have a working example, and some explanations ? 有人有一个工作的例子和一些解释吗?
You have to store the Binding created in SetBinding in your ViewController or it goes away as soon as you leave the scope of ViewDidLoad. 您必须在ViewController中存储在SetBinding中创建的Binding,否则只要您离开ViewDidLoad的范围就会消失。 In the Flowers example, that code only runs during the Loading of the view.
在Flowers示例中,该代码仅在加载视图期间运行。 It's never run due to the change of a value.
由于价值的变化,它永远不会运行。
public partial class MainViewController : UIViewController
{
private Binding<bool, bool> _isUpdatedBinding;
private MainViewModel ViewModel { get; set; }
public MainViewController()
: base("MainViewController", null)
{
this.ViewModel = new MainViewModel();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_isUpdatedBinding = this.SetBinding(() => this.ViewModel.IsUpdated).WhenSourceChanges(() =>
{
this.updateLabel.Text = this.ViewModel.IsUpdated ? "It's okay !" : "Nope ...";
});
this.updateButton.SetCommand("TouchUpInside", this.ViewModel.UpdateCommand);
}
}
I believe these changes should fix your issue. 我相信这些变化应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.