-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] Entry.Focused event is not fired when EditText.FocusChange is set #8731
Comments
@ericc0504 This is a bit of an interesting one :-/ The way Xamarin Android wires up those events is by using a custom OnFocusChangeListener that then propagates that out to FocusChange But in our ViewRenderer we use our own listener to manage FocusChanged which replaces the Xamarin Android one
When you subscribe to the event in your code editText.FocusChange += EditText_FocusChange; That causes the Xamarin Android listener to get swapped back in and then our listener no longer works. I'm not quite sure why we are using a listener here though instead of the events. I'll ask around. Here's some modified code that should resolve what you are doing private void ConfigureControl()
{
Element.PropertyChanged += DroidEntryEffect_PropertyChanged;
}
private void DroidEntryEffect_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if(e.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
EditText editText = ((EditText)Control);
if (editText.HasFocus)
{
Debug.WriteLine("Entry focused. Triggered inside effect.");
Application.Current.MainPage.DisplayAlert("Entry focused", "Triggered inside effect", "OK");
}
}
} |
The reason we use a listener here has to do with reducing allocations. In theory we could subscribe to the FocusChanged event in our code but that would create an allocation for every single subscription so we instead use a listener. A possible approach to resolve this would be to override OnFocusChanged on all our controls and then propagate out from there. We could possibly do something like this inside the ViewRenderer code if (Control is IHasUsefulFocusOverride theOverride)
theOverride.SomeAction = this;
else
Control.OnFocusChangeListener = this; |
Thanks for replying swiftly. The provided solution worked on my side. I am looking forward to the enhancement in the ViewRenderer. |
I stumbled upon this issue because I had a problem with custom entry renderers that have to do FocusChange stuff. It's not strictly related to this GitHub issue, but it's one of the top results on Google for meaningful "xamarin android entry custom renderer onfocuschange" searches, so it might help someone. The code below seems to do the job. Save the original listener, invoke it manually.
And then you get Focused and Unfocused events fired in Xamarin. |
Thanks for this suggestion! As Xamarin.Forms is now in maintenance mode, this will not happen anymore for Xamarin.Forms. We're only adding bugfixes and stability fixes. If this is still important to you, make sure to check the .NET MAUI repo and see if it's already on the roadmap. If not, feel free to open a discussion to discuss a change first or open an issue with a detailed feature request. Thanks! |
Description
When using PlatformEffect on Android and add one more callback on EditText.FocusChange, another event set from Entry.Focused will not be fired.
Steps to Reproduce
Expected Behavior
Both callback from step 1 and step 2 will be called
Actual Behavior
Only the callback from step 2 get called
Basic Information
Reproduction Link
EditTextFocusEventBug.zip
The text was updated successfully, but these errors were encountered: