2016-07-21 8 views
3

私はxamarinとmvvmcrossが新しく、私のiosプロジェクトから私のviewmodelへのシンプルなボタンクリックを結びつけたいと思います。MvvmCross - 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. 
     } 
    } 
} 

私はbtnloginを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; 
     } 
    } 
} 

あなたは私がMyAwesomecommandの名前でmvxCommandを持っているが、私は私の他のプロジェクトでのボタンのクリックからいくつかのロジックを処理したい見ることができるように。誰が私が何をすべきか知っていますか?

答えて

4

私はもっと見渡して、answer hereを見つけました。

MvxCommand _myAwesomeCommand; 

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

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

考え方は、メソッドをパラメータとして使用する新しいコマンドを返すmvxcommandゲッターを持つことです。

ボタンbtnLoginをクリックすると、viewmodelのvoid DoStuffにアクセスできます。

関連する問題