Reputation: 69
i have some problems trying to get the keyboard typing event ...so what i want is execute the command "GoContratoCommand" when the user press the "enter" button on the keyboard
this is part of my 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;
}
}
}
}
and this is my activity(or view as you want to call it) on the path: views/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;
}
}
}
}
i'm stack on this , can you help me to find out how i can run this command?, or if you know another way to use keypress events to call commands i will be grateful if you tell me about it, thanks.
Upvotes: 1
Views: 1259
Reputation: 66882
You can just call Execute
on the Command:
var algo=((Core.ViewModels.FirstViewModel)this.DataContext).GoContratoCommand;
algo.Execute(null);
For reuse, you could also put this code into a helper class:
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;
}
}
}
}
Then this class could be used in your axml as:
<MyEditText
local:MvxBind="KeyCommand TheCommand; Text TheText" />
Upvotes: 3