简体   繁体   English

Xamarin 表单上的按键事件

[英]Keypress Event on Xamarin Forms

I want to fire a keypress event like ENTER and trigger my method but I can't find any reference about doing a keypress event in xaml and call it on my view model我想触发像ENTER这样的按键事件并触发我的方法,但我找不到任何关于在 xaml 中执行按键事件并在我的视图模型上调用它的参考

Could someone throw some reference.有人可以抛出一些参考。 Please thanks.请谢谢。

An input view (Entry or Editor) has to show the Keyboard first, therefore handle the TextChanged event of the input view.输入视图(条目或编辑器)必须首先显示键盘,因此处理输入视图的 TextChanged 事件。

First attach the event:首先附上事件:

    public MainPage()
    {
        InitializeComponent();

        txtEntry.TextChanged += TxtEntry_TextChanged;
    }

Then handle the event然后处理事件

    void TxtEntry_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
        txtEntry.TextChanged -= TxtEntry_TextChanged;
        char key = e.NewTextValue?.Last() ?? ' ';

        if (key == 'A')
        {
            //do something 
        }


        txtEntry.TextChanged += TxtEntry_TextChanged;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM