1

I'm new in using Xamarin and MVVM (Light) and I didn't found a good example on the internet for passing parameters from a view back to a ViewModel so I can execute the functionality there. In other words I'm looking for a way to link my ViewModel and View so I can execute my command from my ViewModel in the view. The problem is that I want to pass parameters with it back. Which seems to be a pain.

I have a very basic 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);

    }

And my View

_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);

Can someone give me a clear and easy example or tell me what I'm doing wrong?

Deff
  • 33
  • 6
  • Are you able to use `XAML`? Also, tis looks like an android view to me, so you might want to tag it with `xamarin.android` so those over there can help you. – Florian Moser Apr 11 '17 at 08:41
  • I'm not using XAML and am only using Android Native, thanks for the tip tho! – Deff Apr 13 '17 at 07:43
  • This is how to pass parameters and getting results back: https://stackoverflow.com/questions/46552891/how-do-i-pass-entry-parameters-to-xamarin-mvvm-viewmodel – Sadjad Khazaie Oct 07 '17 at 21:01

0 Answers0