This is because the Focus function is called elsewhere after calling Test1.Focus.
In AppShell.xaml.cs you can find the following code:
private void OnNavigatedToPage(object sender, NavigationEventArgs e) {
The code above means that when you go to the page, he will sign the event loaded by the page and adjust the focus on the page.
Your code will sign the event loaded by the page on the page itself. And your code will be executed before the Page_Loaded function in AppShell. So you have not received what you want.
So, if you just comment out ((Page)sender).Focus(FocusState.Programmatic); in the function Page_Loaded. You will get what you want. I do not know what the purpose of this line is. But everything seems good.
If you notice something wrong after the comments of this line, we can also process it. Call the focus function once in the LayoutUpdated event after the loaded event.
public sealed partial class BasicPage : Page { bool bAfterLoaded = false; public BasicPage() { this.InitializeComponent(); this.Loaded += BasicPage_Loaded; this.LayoutUpdated += BasicPage_LayoutUpdated; } private void BasicPage_LayoutUpdated(object sender, object e) { if (bAfterLoaded) { Test1.Focus(FocusState.Programmatic); bAfterLoaded = !bAfterLoaded; } } private void BasicPage_Loaded(object sender, RoutedEventArgs e) { bAfterLoaded = !bAfterLoaded; } }
Hope this helps you.
source share