Uwp导航示例和重点控制
Uwp navigation example and focusing on control
我用uwp navigation example as an example for my application navigation. I need to set the focus on TextBox. I try it on uwp navigation example。对于 BasicPage,我添加了这段代码:
<StackPanel Orientation="Vertical">
<TextBox x:Name="Test" />
<TextBox x:Name="Test1" />
</StackPanel>
public BasicPage()
{
this.InitializeComponent();
this.Loaded += BasicPage_Loaded;
}
private void BasicPage_Loaded(object sender, RoutedEventArgs e)
{
Test1.Focus(FocusState.Programmatic);
}
Test1 没有获得焦点。我在 "normal" windows 通用应用程序中尝试此代码 - 它是有效的。你有什么建议?
这是因为你调用Test1.Focus后,Focus函数在其他地方被调用了。
在AppShell.xaml.cs中可以找到如下代码:
private void OnNavigatedToPage(object sender, NavigationEventArgs e)
{
// After a successful navigation set keyboard focus to the loaded page
if (e.Content is Page && e.Content != null)
{
var control = (Page)e.Content;
control.Loaded += Page_Loaded;
}
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
((Page)sender).Focus(FocusState.Programmatic);
((Page)sender).Loaded -= Page_Loaded;
this.CheckTogglePaneButtonSizeChanged();
}
以上代码表示当你导航到一个页面时,它会订阅页面加载事件并将焦点设置在页面上。
您的代码在页面本身中订阅页面加载事件。并且您的代码将在 AppShell 中的 Page_Loaded 函数之前执行。所以你没有得到你想要的。
所以如果你简单地在Page_Loaded函数中注释掉((Page)sender).Focus(FocusState.Programmatic);
。你会得到你想要的。我不确定那条线的确切目的是什么。但一切似乎都很好。
如果您在注释掉该行后确实发现问题,我们也可以解决。加载事件后在 LayoutUpdated 事件中调用一次焦点函数。
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;
}
}
希望对您有所帮助。
如果您想以编程方式聚焦文本框。防止键盘显示,这样 layoutupdated 事件就不会触发。你可以做类似的事情
然后在 page_loaded 事件中执行 Test1.Focus(FocusState.Programmatic);
我用uwp navigation example as an example for my application navigation. I need to set the focus on TextBox. I try it on uwp navigation example。对于 BasicPage,我添加了这段代码:
<StackPanel Orientation="Vertical">
<TextBox x:Name="Test" />
<TextBox x:Name="Test1" />
</StackPanel>
public BasicPage()
{
this.InitializeComponent();
this.Loaded += BasicPage_Loaded;
}
private void BasicPage_Loaded(object sender, RoutedEventArgs e)
{
Test1.Focus(FocusState.Programmatic);
}
Test1 没有获得焦点。我在 "normal" windows 通用应用程序中尝试此代码 - 它是有效的。你有什么建议?
这是因为你调用Test1.Focus后,Focus函数在其他地方被调用了。
在AppShell.xaml.cs中可以找到如下代码:
private void OnNavigatedToPage(object sender, NavigationEventArgs e)
{
// After a successful navigation set keyboard focus to the loaded page
if (e.Content is Page && e.Content != null)
{
var control = (Page)e.Content;
control.Loaded += Page_Loaded;
}
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
((Page)sender).Focus(FocusState.Programmatic);
((Page)sender).Loaded -= Page_Loaded;
this.CheckTogglePaneButtonSizeChanged();
}
以上代码表示当你导航到一个页面时,它会订阅页面加载事件并将焦点设置在页面上。
您的代码在页面本身中订阅页面加载事件。并且您的代码将在 AppShell 中的 Page_Loaded 函数之前执行。所以你没有得到你想要的。
所以如果你简单地在Page_Loaded函数中注释掉((Page)sender).Focus(FocusState.Programmatic);
。你会得到你想要的。我不确定那条线的确切目的是什么。但一切似乎都很好。
如果您在注释掉该行后确实发现问题,我们也可以解决。加载事件后在 LayoutUpdated 事件中调用一次焦点函数。
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;
}
}
希望对您有所帮助。
如果您想以编程方式聚焦文本框。防止键盘显示,这样 layoutupdated 事件就不会触发。你可以做类似的事情 然后在 page_loaded 事件中执行 Test1.Focus(FocusState.Programmatic);