question

SWAPNILGAIKWAD-3135 avatar image
0 Votes"
SWAPNILGAIKWAD-3135 asked RobCaplan edited

when i enter some text on password field and tap on outside of the password field again come back to the password field previous text is getting cleared, i don't want to clear the previous text.

when i enter some text on password field and tap on outside of the password field again come back to the password field previous text is getting cleared, i don't want to clear the previous text.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Hello @SWAPNILGAIKWAD-3135 ,​

Welcome to our Microsoft Q&A platform!

To prevent changing the pervious text when focusing the entry, try to create a custom Entry renderer to set UITextFieldDelegate for it. You could override the ShouldChangeCharacters method to change the behavior in the custom delegate class.

Check the code:

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace TestApplication_6.iOS
{
    public class CustomEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control.SecureTextEntry)
            {
                Control.Delegate = new CustomTextFieldDelegate();
            }
        }
    }

    internal class CustomTextFieldDelegate : UITextFieldDelegate
    {
        public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
        {
            string updatedString = textField.Text.Substring(0, (int)range.Location) + replacementString + textField.Text.Substring((int)(range.Location + range.Length));
            textField.Text = updatedString;

            return false;
        }
    }
}


Best Regards,

Jarvan 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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.