xamarin.forms - How to detect when user backs out of a text Entry without pressing enter in Xamarin Forms? -
in xamarin forms app have page entry
view entering text. i've wired textchanged
event ui dynamically updates user enters text, , want updated ui persist if user presses enter (i.e. completed
event triggered) revert how if user backs out of view (i.e. unfocused
event triggered not completed
event).
the complication here - @ least on android - xamarin triggers unfocused
event first (in both cases) , then completed
event (only in case user presses enter). it's difficult know when revert ui changes. counter-intuitive me: when press enter view still has focus, doesn't make sense unfocused
called first before completed
.
some things i've tried , not happy with:
- inside
unfocused
event handler, start timer 200ms, , ifcompleted
event hasn't happened assume it's not going , revert ui (doesn't seem reliable, 200ms totally arbitrary). - always revert ui inside
unfocused
event , reapply changes incompleted
event if received (ugly, causes ui flicker, bad performance). - some combination of above, e.g: start timer , revert ui changes after 200ms, reapply them if
completed
received after or cancel timer if received before (getting bit complicated).
what's proper way this?
changing suggestion of timer little bit. "entryfocus" entry name.
cancellationtokensource source; private void entryfocus_completed(object sender, eventargs e) { try { source.cancel(); } catch { } } async private void entryfocus_unfocused(object sender, focuseventargs e) { source = new cancellationtokensource(); try { await task.delay(500, source.token); } catch (taskcanceledexception) //completed event requested cancelation { return; } catch { } //something wrong entryfocus.text = ""; }
Comments
Post a Comment