我是使用Xamarin和MVVM(Light)的新手,我没有在互联网上找到一个很好的例子,可以将参数从视图传递回ViewModel,因此我可以在那里执行功能。 换句话说,我正在寻找一种方法来链接我的ViewModel和View,这样我就可以在视图中从我的ViewModel执行我的命令。 问题是我想用它传回参数。这似乎是一种痛苦。
我有一个非常基本的ViewModel
public ICommand RegisterUserAsyncCommand { get; set; }
private async void RegisterUserAsync(User userInfo)
{
string firstName = userInfo.FirstName;
string lastName = userInfo.LastName;
string email = userInfo.Email;
string password = userInfo.Password;
bool admin = userInfo.Admin;
//params = int, string, string, string, string, bool
User newUser = new User(null, firstName, lastName, email, password, admin);
}
public RegisterViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
_userDataService = new UserDataService(new UserWebservice());
//Commands
RegisterUserAsyncCommand = new RelayCommand<User>(RegisterUserAsync);
}
我的观点
_firstNameTextView = FindViewById<TextView>(Resource.Id.activityRegisterFirstNameTextView);
_lastNameTextView = FindViewById<TextView>(Resource.Id.activityRegisterLastNameTextView);
_emailTextView = FindViewById<TextView>(Resource.Id.activityRegisterEmailTextView);
_passwordTextView = FindViewById<TextView>(Resource.Id.activityRegisterEmailTextView);
_confirmTextView = FindViewById<TextView>(Resource.Id.activityRegisterConfirmPasswordTextView);
_registerButton = FindViewById<Button>(Resource.Id.activityRegisterRegisterButton);
Binding registerBinding = this.SetBinding(() => ViewModel.Registration, () => new Registration(_firstNameTextView.Text, _lastNameTextView.Text, _emailTextView.Text, _passwordTextView.Text, _confirmTextView.Text, true)
);
_registerButton.SetCommand("Click", (GalaSoft.MvvmLight.Command.RelayCommand<GalaSoft.MvvmLight.Helpers.Binding>)ViewModel.RegisterUserAsyncCommand, registerBinding);
有人可以给我一个明确而简单的例子或者告诉我我做错了什么吗?