我有Page,当我点击它的空白部分时,它会失去焦点。我尝试将边框作为背景,但是当我点击它时也会失去焦点。为什么会这样?

我真正需要做的是在用户打开AppBar或设置超级按钮时禁用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事件。更好的是,您可以在同一LostFocus上设置RootGrid事件。然后它可能会正常工作。请尝试一下。
谢谢。

关于c# - 为什么我的页面失去焦点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14875964/

10-09 00:44