简体   繁体   English

为什么我的页面会失去焦点?

[英]Why does my Page lose focus?

I have Page, and it loses focus when I tap on an empty part of it. 我有页面,当我点击它的空白部分时,它会失去焦点。 I tried putting a Border as the background, but that loses focus too when I tap it. 我尝试将边框作为背景,但是当我点击它时,它也会失去焦点。 Why does this happen? 为什么会这样?

What I really need to do is disable a WebView when the user opens the AppBar or the Settings Charm 我真正需要做的是在用户打开AppBar或Settings Charm时禁用WebView

Some example code to demonstrate the problem (watch the output window): 一些示例代码来演示问题(观察输出窗口):

XAML: XAML:

<Page
    x:Name="Pagey"
    x:Class="FocusTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FocusTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" GotFocus="Focus" LostFocus="LoseFocus">

    <Grid x:Name="RootGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" GotFocus="Focus" LostFocus="LoseFocus">
        <StackPanel>
            <Button x:Name="Clicky" Content="Clicky" GotFocus="Focus" LostFocus="LoseFocus" HorizontalAlignment="Center"></Button>
            <Border x:Name="Border" Width="100" Height="100" Background="Red" GotFocus="Focus" LostFocus="LoseFocus"></Border>
            <Button x:Name="Clicky2" Content="Clicky2" GotFocus="Focus" LostFocus="LoseFocus" HorizontalAlignment="Center"></Button>
        </StackPanel>
    </Grid>
</Page>

Code behind: 代码背后:

using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace FocusTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        void Focus(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Focus({0})", (sender as FrameworkElement).Name);
        }

        void LoseFocus(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("LoseFocus({0})", (sender as FrameworkElement).Name);
        }
    }
}

Look like your Border is inside the RootGrid So that every time when you tap Border's tap event will occur and LostFocus Event fired. 看起来你的BorderRootGrid里面所以每当你点击Border's tap event时,就会发生Border's tap event并且LostFocus事件被触发。

Better you can set LostFocus event on the same RootGrid . 更好的是你可以在同一个RootGrid上设置LostFocus事件。 Then it may work fine. 然后它可能工作正常。
Please try it. 请试一试。 Thanks. 谢谢。

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

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