In my Xamarin Forms app I have a page with an Entry
view for entering text. I've wired up the TextChanged
event so that the UI dynamically updates as the user enters text, and I want the updated UI to persist if the user presses enter (i.e. the Completed
event is triggered) but revert to how it was previously if the user backs out of the view (i.e. the Unfocused
event is triggered but not the Completed
event).
The complication here is that - at least on Android - Xamarin triggers the Unfocused
event first (in both cases) and then the Completed
event (only in the case where the user presses enter). So it's difficult to know exactly when to revert the UI changes. This is counter-intuitive to me: when I press enter the view obviously still has focus, so it doesn't make sense for Unfocused
to be called first before the Completed
.
Some things I've tried and am not happy with:
- Inside the
Unfocused
event handler, start a timer for 200ms, and if theCompleted
event hasn't happened by then assume it's not going to and revert the UI (doesn't seem reliable, 200ms is totally arbitrary). - Always revert the UI inside the
Unfocused
event and then reapply the changes in theCompleted
event if it is received (ugly, causes UI to flicker, bad performance). - Some combination of the above, e.g: start a timer and then revert the UI changes after 200ms, but reapply them if
Completed
is received after that or cancel the timer if it is received before then (getting a bit complicated).
What's the proper way to do this?