nick gowdy
nick gowdy

Reputation: 6511

MvvmCross - handle button click in viewmodel

I'm new to xamarin and mvvmcross and I would like to wire up a simple button click from my ios project to my viewmodel.

using System;
using MvvmCross.Binding.BindingContext;
using MvvmCross.iOS.Views;
using Colingual.Core.ViewModels;

namespace Colingual.iOS
{
    public partial class LoginView : MvxViewController
    {
        public LoginView() : base("LoginView", null)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            var set = this.CreateBindingSet<LoginView, LoginViewModel>();
            set.Bind(Username).To(vm => vm.Username);
            set.Bind(Password).To(vm => vm.Password);
            set.Bind(btnLogin).To(vm => vm.MyAwesomeCommand);
            set.Apply();
        }


        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }
}

I would like to wire up btnlogin to myawesomecommand.

using MvvmCross.Core.ViewModels;

namespace Colingual.Core.ViewModels
{
    public class LoginViewModel : MvxViewModel
    {
        readonly IAuthenticationService _authenticationService;

        public LoginViewModel(IAuthenticationService authenticationService)
        {
            _authenticationService = authenticationService;
        }

        string _username = string.Empty;
        public string Username
        {
            get { return _username; }
            set { SetProperty(ref _username, value); }
        }

        string _password = string.Empty;
        public string Password
        {
            get { return _password; }
            set { SetProperty(ref _password, value); }
        }



        public bool AuthenticateUser() {
            return true;
        }

        MvxCommand _myAwesomeCommand;


        public IMvxCommand MyAwesomeCommand
        {
            get
            {
                DoStuff();
                return _myAwesomeCommand;
            }
        }

        void DoStuff()
        {
            string test = string.Empty;
        }
    }
}

As you can see I've got mvxCommand with the name of MyAwesomecommand but I want to handle some logic from a button click in my other project. Anyone know what I should do?

Upvotes: 3

Views: 4046

Answers (1)

nick gowdy
nick gowdy

Reputation: 6511

I've done more looking around and found an answer here.

MvxCommand _myAwesomeCommand;

        public IMvxCommand MyAwesomeCommand
        {
            get { return new MvxCommand(DoStuff); }
        }

        void DoStuff()
        {
            string test = string.Empty;
        }

The idea is to have your mvxcommand getter which returns a new command that takes the method as a parameter.

When clicking the button btnLogin, you can access void DoStuff in viewmodel.

Upvotes: 6

Related Questions