Xamarin.iOS中使用MvvmLight框架
Xamarin.iOS中使用MvvmLight框架
如何在Xamarin.iOS 中使用MvvmLight框架:
1. 通过NuGet包管理器安装MvvmLight包
2. 创建对应的ViewModel
3. 在Controller中使用ViewModel(需要在Controller中添加引用:using GalaSoft.MvvmLight.Helpers 才能使用binding技术)
UpdateTargetTrigger方法已经不被推荐使用,推荐使用ObserveTargetEvent。
ViewModel:
public class TextViewModel : ViewModelBase { private string text; public string Text { get { return text; } set {
if (value != _text)
{
_text= value;
RaisePropertyChanged("Text");
}
} } }
UIViewController:
需要在Controller中添加引用:
using GalaSoft.MvvmLight.Helpers;
public class TextViewController : UIViewController { private TextViewModel textViewModel; private Binding<string, string> textFieldBinding; private UITextField textField; private UITextField TextField { get { return textField; } } private TextViewModel TextViewModel { get { return textViewModel; } } public override void ViewDidLoad() { base.ViewDidLoad(); textViewModel = new TextViewModel(); textField = new UITextField(new CoreGraphics.CGRect(20, 75, 280, 40)); textField.BackgroundColor = UIColor.White; textFieldBinding = this.SetBinding( () => TextViewModel.Text, () => TextField.Text, BindingMode.TwoWay) .ObserveTargetEvent(nameof(UITextField.EditingChanged)); View.AddSubview(textField); } }
UISlider:
this.SetBinding(() => mDetailViewModel.PasswordLength, () => SliderCount.Value, BindingMode.TwoWay).ObserveTargetEvent(nameof(UISlider.ValueChanged));
BEMCheckBox:
this.SetBinding(() => mDetailViewModel.CheckLower, () => CheckBoxContainer.CheckBoxLower.On, BindingMode.TwoWay).ObserveTargetEvent(nameof(BEMCheckBox.AnimationDidStopForCheckBox));