xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

Focused and UnFocused event of the Editor is not triggered when Editor’s renderer (EditText) is added as custom renderer in Xamarin Forms Android #4231

Open Ganeshamoorthy10 opened 6 years ago

Ganeshamoorthy10 commented 6 years ago

Description

In my scenario I have added Editor’s renderer (EditText) as custom renderer for my custom layout, FousChanged event of EditText triggers properly but Focused and UnFocused event of Editor is not triggering. When I analyzed the issue, Focused and UnFocused event will be triggered when IsFocused property of Editor is True, in my scenario IsFocused property is not assigned to True. Since, it is read only property I cannot manually assign IsFocused as true. Could you please suggest any override method to check the IsFocused property and also, please provide details about when IsFocused will be assigned as True. Kindly find the code snippet and sample below,

Code snippet:

public class CustomEditor : Editor
 {
 }

CustomEditor customEditor = new CustomEditor();


protected override void OnElementChanged(ElementChangedEventArgs<CustomStack> e)

{

Android.Widget.ScrollView stack = new Android.Widget.ScrollView(Context);

if (e.NewElement != null)

{

var renderer = Platform.CreateRenderer(customEditor);

var nativeEditor = RendererHelper.GetNativeView(renderer.View);


(nativeEditor as EditText).FocusChange += Handle_FocusChange;

nativeEditor.RemoveFromParent();

stack.AddView(nativeEditor);


SetNativeControl(stack);

}
 base.OnElementChanged(e);
 }

Sample:
CustomEditor.zip

Note: This scenario works properly in Xamarin Forms UWP and iOS.

Regards, Ganeshamoorthy C

pauldipietro commented 6 years ago

Updated the sample to 3.4 and at a cursory glance the IsFocused value is false, so the behavior is as described until further investigation.