我最近修复了首次加载主窗口时通过情节提要显示的登录启动屏幕的问题。
我相信有两把钥匙可以解决问题。一种是使包含元素成为焦点范围。另一个是处理由加载的窗口触发的情节提要的“情节提要已完成”事件。
这个故事板使用户名和密码画布可见,然后逐渐变为100%不透明。关键是用户名控件在脚本运行之前是不可见的,因此该控件在可见之前无法获得键盘的焦点。让我有一段时间感到困惑的是,它有“焦点”(即焦点是真的,但事实证明这只是逻辑焦点),直到阅读Kent Boogaart的答案并查看微软的WPF链接文本
一旦我这样做了,我的特定问题的解决方案就很简单:
1) 使包含的元素成为焦点范围
<Canvas FocusManager.IsFocusScope="True" Visibility="Collapsed">
<TextBox x:Name="m_uxUsername" AcceptsTab="False" AcceptsReturn="False">
</TextBox>
</Canvas>
2) 将已完成的事件处理程序附加到情节提要
<Storyboard x:Key="Splash Screen" Completed="UserNamePassword_Storyboard_Completed">
...
</Storyboard>
和
3) Set my username TextBox to have the keyboard focus in the storyboard completed event h和ler.
void UserNamePassword_Storyboard_Completed(object sender, EventArgs e)
{
m_uxUsername.Focus();
}
Note that calling item.Focus() results in the call Keyboard.Focus(this), so you don t need to call this explicitly. See this question about the difference between Keyboard.Focus(item) 和 item.Focus.