为什么我的页面失去焦点

本文关键字:失去 焦点 我的 为什么 | 更新日期: 2023-09-27 17:59:22

我有一个Page,当我点击它的空白部分时,它会失去焦点。我试着把一个Border作为背景,但当我点击时,它也失去了焦点。为什么会发生这种情况?

我真正需要做的是当用户打开AppBar或Settings Charm 时禁用WebView

一些演示问题的示例代码(注意输出窗口):

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>

代码背后:

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);
        }
    }
}

为什么我的页面失去焦点

看起来您的BorderRootGrid内部,因此每次点击Border's tap event时都会发生LostFocus事件

您可以更好地在同一个RootGrid上设置LostFocus事件。那么它可能会正常工作
请尝试一下。谢谢