Entry.ReturnCommand skipped when keyboard loses focus

ffrige 1 Reputation point
2021-04-01T02:58:08.307+00:00

I use an Entry control to edit a text and then I execute the ReturnCommand to check and modify its content. That all worked fine until I found out that when the users taps away from the input keyboard the text is saved but the ReturnCommand is not executed.

It seems there are no other commands that can be triggered when the Entry is completed. Is there a solution to that?

I cannot use the set property of the variable in the binding because that would modify the text while the user is typing, which makes a big mess.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,701 Reputation points Microsoft Vendor
    2021-04-01T09:09:19.41+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    That all worked fine until I found out that when the users taps away from the input keyboard the text is saved but the ReturnCommand is not executed.

    When we check document Customize the return key, we will find that :

    The appearance of the return key on the soft keyboard, which is displayed when an Entry has focus, can be customized by setting the ReturnType property to a value of the ReturnType enumeration:

    • Default – indicates that no specific return key is required and that the platform default will be used.
    • Done – indicates a "Done" return key.
    • Go – indicates a "Go" return key.
    • Next – indicates a "Next" return key.
    • Search – indicates a "Search" return key.
    • Send – indicates a "Send" return key.

    And when the return key is pressed, the Completed event fires and any ICommand specified by the ReturnCommand property is executed.

    So, the ReturnCommand is triggered only when the return key on the soft keyboard is clicked. That's why when you taps away from the input keyboard, the event didn't fire.

    I cannot use the set property of the variable in the binding because that would modify the text while the user is typing, which makes a big mess.

    For this ,we usually set property of the variable in the binding to achieve this funtion just as you mendtioned.
    For more details, you can refer to the official document: The Xamarin.Forms Command Interface .

    In addition, what do you mean by words a big mess? Can you explain it in detail?

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. ffrige 1 Reputation point
    2021-04-01T10:50:11.85+00:00

    Hello Jessie,

    Thank you for your answer. What I meant to say is that in certain cases it is not convenient to check and modify the text while the user is still typing. It would be much better to have a way to wait until the end, i.e. when the return key is pressed or when the keyboard loses focus.

    To give you a practical example: I have entries for user IDs. Each user can enter an ID, which is accepted only if no other user has entered the same value before. When checking that at the end of the entry process I can reset the value to zero and explain the reason with a pop-up message. However, if I have to check that while the user is typing it gets messy. Imagine for instance that one user already entered the ID "1". Now another user wants to have the ID "15", which is totally valid. But as soon as he starts typing the first digit "1" the value gets deleted and an error pops up saying that the ID "1" has already been taken.

    I think the cleanest solution would be either to allow the keyboard to disappear only if the return key is pressed (and not by just tapping away from it), or to always trigger the return key whenever the keyboard closes (no matter what the reason was for it to close).

    I hope you understand what I mean.
    Thank you very much for your help!
    Fabrizio