question

XamBoy avatar image
1 Vote"
XamBoy asked KyleWang-MSFT answered

Hot to disable entry autofocus on page appearing?


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


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

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered

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.

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.