2014-04-22 58 views
1

我有一些問題,試圖讓鍵盤打字事件...所以我要的是 執行命令「GoContratoCommand」當用戶按下鍵盤如何在android中使用mvvmcross中的按鍵事件?

上的「進入」按鈕,這是我的ViewModel的一部分:

using System; 
using System.Collections.Generic; 
using Cirrious.MvvmCross.ViewModels; 
using MultiPage.Core.Services; 
namespace MultiPage.Core.ViewModels 
{ 
public class FirstViewModel 
    : MvxViewModel 
    { 



    private Cirrious.MvvmCross.ViewModels.MvxCommand _goContratoCommand; 
    public System.Windows.Input.ICommand GoContratoCommand 
    { 
     get{ 

       _goContratoCommand = _goContratoCommand ?? new Cirrious.MvvmCross.ViewModels.MvxCommand(inv_prod_pareja); 
        return _goContratoCommand; 
       } 

     } 


    } 
    } 

,這是我的活動(或視圖,你想將它命名)的路徑:查看/ FirstView.cs

using Android.App; 
using Android.OS; 
using Android.Widget; 
using Cirrious.MvvmCross.Droid.Views; 

namespace MultiPage.Droid.Views 
{ 
    [Activity(Label = "View for FirstViewModel")] 
    public class FirstView : MvxActivity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      //SetContentView(Resource.Layout.FirstView); 
      SetContentView(Resource.Layout.screen01); 
      var contrato= FindViewById<EditText>(Resource.Id.txtMembershipNumber); 
      contrato.EditorAction += EventHandlerContrato; 
     } 
     public void EventHandlerContrato(object MyObject,EditText.EditorActionEventArgs e){ 
      e.Handled = false; 
      if (e.ActionId == Android.Views.InputMethods.ImeAction.ImeNull || 
       e.ActionId == Android.Views.InputMethods.ImeAction.Next || 
       e.ActionId == Android.Views.InputMethods.ImeAction.Unspecified || 
       e.ActionId == Android.Views.InputMethods.ImeAction.None || 
       e.ActionId == Android.Views.InputMethods.ImeAction.Send || 
       e.ActionId == Android.Views.InputMethods.ImeAction.Go) 
      { 
       var algo=((Core.ViewModels.FirstViewModel)this.DataContext).GoContratoCommand; 
       e.Handled = true; 

      } 
     } 
    } 


    } 

我在這堆,你可以幫我瞭解我如何運行此com mand ?,或者如果你知道 另一種使用按鍵事件來調用命令的方法,我將不勝感激,如果你告訴我,謝謝。

回答

2

您只需撥打Execute的命令:

  var algo=((Core.ViewModels.FirstViewModel)this.DataContext).GoContratoCommand; 
      algo.Execute(null); 

重用,你也可以把這個代碼放到一個輔助類:

public class MyEditText : EditText 
{ 
    public MyEditText(Context c, IAttributeSet a) : base(c,a) { 
     this.EditorAction += EventHandlerContrato; 
    } 

    public ICommand KeyCommand { get;set; } 

    public void EventHandlerContrato(object MyObject,EditText.EditorActionEventArgs e){ 
     e.Handled = false; 
     if (e.ActionId == Android.Views.InputMethods.ImeAction.ImeNull || 
      e.ActionId == Android.Views.InputMethods.ImeAction.Next || 
      e.ActionId == Android.Views.InputMethods.ImeAction.Unspecified || 
      e.ActionId == Android.Views.InputMethods.ImeAction.None || 
      e.ActionId == Android.Views.InputMethods.ImeAction.Send || 
      e.ActionId == Android.Views.InputMethods.ImeAction.Go) 
     { 
      if (KeyCommand != null) { 
       KeyCommand.Execute(null); 
       e.Handled = true; 
      } 
     } 
    } 
} 

那麼可以使用這個類在你axml如:

<MyEditText 
    local:MvxBind="KeyCommand TheCommand; Text TheText" /> 
+0

耶穌!!!,噸很多,並感謝KeyCommand的一角,我也在尋找一種方法來做到這一點,謝謝你!我是一個新手 –

相關問題