简体   繁体   中英

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

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.

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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