2016-07-21 43 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; 
     } 
    } 
} 

正如你可以看到我有mvxCommand與MyAwesomecommand的名字,但我想從我在其他項目中點擊一個按鈕處理一些邏輯。任何人都知道我該怎麼辦?

回答

4

我已經做了更多的四處張望,發現一個answer here

MvxCommand _myAwesomeCommand; 

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

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

的想法是讓你mvxcommand的getter返回一個新的命令服用方法作爲參數。

單擊按鈕btnLogin時,可以訪問viewmodel中的無效DoStuff。