父页的 keyDown 事件即使在用户控件中处理后也会触发



我有一个带有CoreWindow_KeyDown事件的IndexPage。当我按下一个键时,它就会启动。没错。如果我在包含PreviewKeyDown/KeyDown事件的用户控件上按键,索引页的CoreWindow_KeyDown事件也会与用户控件的PreviewKeyDown/KeyDown事件一起触发。

UserControl_PreviewKeyDown, e.Handled = true
UserControl_KeyDown, e.Handled = true

如果由用户控件处理,如何防止IndexPage触发CoreWindow_KeyDown事件?

Parent Page的keyDown事件即使在用户控件中处理后也会触发

根据设计,将e.Handled设置为UserControl_PreviewKeyDown的true不会禁用到CoreWindow的路由事件气泡。

对于您的需求,由于PreviewKeyDown的触发条件是控件需要聚焦,我们可以声明bool属性,并在控件聚焦时将其设置为true。并使用此bool属性禁用CoreWindow_KeyDown事件中的进程逻辑。

private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if (!_isFocus)
{
System.Diagnostics.Debug.WriteLine("---------!_isFocus-----------");
}
else
{
System.Diagnostics.Debug.WriteLine("---------_isFocus-----------");
}
}
private bool _isFocus;
private void MyCC_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
_isFocus = true;
}
private void MyCC_LosingFocus(UIElement sender, LosingFocusEventArgs args)
{
_isFocus = false;
}

最新更新