简体   繁体   中英

Is it possible binding a property from VM to a local property in the controller, using MVVMCross 4 and Xamarin.iOS

I have a local string variable in my controller and would like to bind it to a property in my ViewModel (core). I have tried:

private string culture;
var bindings = this.CreateBindingSet<OnBoardingViewController, OnBoardingViewModel>();
bindings.Bind(culture).For(x => x).To(viewmodel => viewmodel.LanguageCode);
bindings.Apply();

It doesn't succeed. Is it possible bind a local variable using MvvmCross or just components properties?

You can create a Property MyProperty on your ViewController and bind it like:

var bindings = this.CreateBindingSet<OnBoardingViewController, OnBoardingViewModel>();
bindings.Bind(this).For(x => x.MyProperty).To(viewmodel => viewmodel.LanguageCode);
bindings.Apply();

MvvMCross needs the expression in For to extract the property name. And this expression has to be in the form x => x.Property , or x => x.Property.SubProperty , etc. And the second problem is, that local variables are -as their name suggests- local. So they will get cleaned up, when you leave the function. That's why I strongly recommend introducing a extra property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM