简体   繁体   English

如何防止 Esc 键关闭弹出窗口?

[英]How to prevent Esc key from light dismissing a popup?

I have a Popup which needs to be light dismissed when user taps an element outside the popup.我有一个弹出窗口,当用户点击弹出窗口外的元素时需要将其关闭。 By default, pressing the Esc key also triggers LightDismiss, which I would like to prevent.默认情况下,按 Esc 键也会触发 LightDismiss,我想阻止这种情况。 Instead, I'd like to handle this KeyDown event explicitly.相反,我想明确地处理这个 KeyDown 事件。

Handling the Tapped event in the page's root to check if user's tapped outside the popup is one solution, but since every tap in my app will go through this, I'd prefer not to take this approach.处理页面根中的 Tapped 事件以检查用户是否在弹出窗口外点击是一种解决方案,但由于我的应用程序中的每次点击都会通过此 go,我不想采用这种方法。

Some pseudo-code:一些伪代码:

<Page>
    <Grid>
        <Popup
            x:Name="FooTip"
            Opened="FooTip_Opened"
            Closed="FooTip_Closed"
            IsLightDismissEnabled="True"
            ShouldConstrainToRootBounds="True">
            <!-- Popup content here -->
        </Popup>
    </Grid>
</Page>

TLDR: FooTip should not get light dismissed when Esc key is pressed. TLDR:当按下 Esc 键时,FooTip 不应熄灭。

FooTip should not get light dismissed when Esc key is pressed.当按下 Esc 键时,FooTip 不应消失。

You could handle the PreviewKeyDown event for the Page like this:您可以像这样处理PagePreviewKeyDown事件:

private void OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Escape
        && FooTip.IsOpen)
    {
        e.Handled = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM