Hot to disable entry autofocus on page appearing?

eduardk 46 Reputation points
2021-06-02T10:08:46.583+00:00

I have a shell app with a bottom TabBar. Bottom TabBar has 5 ShellContent pages. Each page has several entry forms.

Here is my problem:
If I use the entry control on page 1 (adding text), and after I navigate to page 2 and back to page 1 the entry on page 1 automatically gains focus (the cursor start blinking without showing the keyboard). How to disable the entry control autofocus on page appearing in android (I don't know if ios has the same problem)?
101648-svid-20210602-125539-11.gif

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,380 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-06-03T05:21:25.273+00:00

    Hi XamBoy,

    Welcome to our Microsoft Q&A platform!

    In my test, if the text in the entry is modified, the "Focused" event will be triggered when you back to the page again.

    So here I have a workaround that you can create a static field to save the count of entries whose text has been modified.

    static class EntryEditedCounterClass  
    {  
        public static List<int> editedEntryIndex = new List<int>();  
    }  
    

    Then subscribe to event "entry_Focused" and "entry_TextChanged" for each entry.

    <StackLayout>  
        <Entry x:Name="entry1" Focused="entry_Focused" TextChanged="entry_TextChanged"/>  
        <Entry x:Name="entry2" Focused="entry_Focused" TextChanged="entry_TextChanged"/>  
        <Entry x:Name="entry3" Focused="entry_Focused" TextChanged="entry_TextChanged"/>  
    </StackLayout>  
    

    You also need to override "OnAppearing" to reset the "editedEntryIndex" when you reopen the page.

    Here is a simple demo you can refer to.

    int entrycount;  
      
    protected override void OnAppearing()  
    {  
        base.OnAppearing();  
        // reset editedEntryIndex   
        entrycount = EntryEditedCounterClass.editedEntryIndex.Count;  
    }  
      
    private void entry_Focused(object sender, FocusEventArgs e)  
    {  
        // if focused, call Unfocus()  
        if (entrycount > 0)  
        {  
            (sender as Entry).Unfocus();  
            entrycount--;  
        }  
    }  
      
    private void entry_TextChanged(object sender, TextChangedEventArgs e)  
    {  
        // add new index to editedEntryIndex  
        var entry = sender as Entry;  
        var entrylist = (entry.Parent as StackLayout).Children;  
        var entryindex = entrylist.IndexOf(entry);  
      
        if (EntryEditedCounterClass.editedEntryIndex.Contains(entryindex))  
        {  
            return;  
        }  
        else  
        {  
            EntryEditedCounterClass.editedEntryIndex.Add(entryindex);  
        }  
    }  
    

    Regards,
    Kyle


    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.