如何在Android的mvvmcross中使用按键事件? [英] how to use keypress events in mvvmcross for android?
问题描述
我在尝试获取键盘打字事件时遇到了一些问题...所以我想要的是 当用户按下键盘上的输入"按钮时,执行命令"GoContratoCommand"
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
这是我的ViewModel的一部分:
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;
}
}
}
}
这是我在以下路径上的活动(或您要调用的视图):views/FirstView.cs
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.
推荐答案
您只需在命令上调用Execute
:
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;
}
}
}
}
然后该类可以在您的axml中用作:
Then this class could be used in your axml as:
<MyEditText
local:MvxBind="KeyCommand TheCommand; Text TheText" />
这篇关于如何在Android的mvvmcross中使用按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!