[英]Software Keyboard Dissapears from screen when PasswordBox given focus
I have a PasswordBox on a page which I am trying to set to automatically have focus when navigating to the page. 我在页面上设置了一个PasswordBox,试图将其设置为导航到该页面时自动具有焦点。
I am having problems where when I give it focus programmatically, it accepts the focus, but the keyboard goes away. 我遇到的问题是,当我以编程方式使其聚焦时,它可以接受聚焦,但是键盘消失了。
This causes a problem as the user must click off the PasswordBox, and then back on to use the control. 这会导致出现问题,因为用户必须单击“密码框”,然后再使用该控件。
I have tried adding this code in the page's Loaded event, the ContentGrid.Loaded, OnNavigatedTo, and they all produce the same result. 我试过将此代码添加到页面的Loaded事件,ContentGrid.Loaded,OnNavigatedTo中,并且它们都产生相同的结果。
I have tried setting the TabIndex/IsTabStop of the page, and the control itself, but it does not seem to work. 我尝试设置页面的TabIndex / IsTabStop以及控件本身,但是它似乎不起作用。 The Passwordbox is the only item which has a TabIndex.
密码框是唯一具有TabIndex的项目。
<PasswordBox x:Name="pwbAnswer" Style="{StaticResource PasswordBoxStyle}" VerticalAlignment="Top" Grid.Row="3"
PasswordChanged="pwbAnswer_PasswordChanged" KeyUp="pwbAnswer_KeyUp" TabIndex="1" IsTabStop="True" />
private void ContentGrid_Loaded(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
pwbAnswer.Focus();
}
You have to use the Loaded Event of the PasswordBox. 您必须使用PasswordBox的Loaded事件。 I had the same problem.
我有同样的问题。 And than you can set the Focus to the sender, which is the PasswordBox itself, if you attached to that loading event.
然后,如果您附加了该加载事件,则可以将焦点设置为发件人,也就是PasswordBox本身。
<PasswordBox x:Name="pwbAnswer" Style="{StaticResource PasswordBoxStyle}" VerticalAlignment="Top" Grid.Row="3"
Loaded="PasswordBox_Loaded" PasswordChanged="pwbAnswer_PasswordChanged" KeyUp="pwbAnswer_KeyUp" TabIndex="1" IsTabStop="True" />
private void PasswordBox_Loaded(object sender, RoutedEventArgs e)
{
PasswordBox box = sender as PasswordBox;
box.Focus();
}
or you can use the workaround with the LayoutUpdated Event. 或者您可以将解决方法与LayoutUpdated事件一起使用。
<Page .... LayoutUpdated="ContentGrid_LayoutUpdated">
<PasswordBox x:Name="pwbAnswer" Style="{StaticResource PasswordBoxStyle}" VerticalAlignment="Top" Grid.Row="3"
KeyUp="pwbAnswer_KeyUp" TabIndex="1" IsTabStop="True" />
private void ContentGrid_LayoutUpdated(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
pwbAnswer.Focus();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.