Why is my page losing focus?

I have a page and it loses focus when I click on a blank part. I tried to put Border as the background, but that also loses focus when I click it. Why is this happening?

I really need to disable the WebView when the user opens the AppBar or Charm Settings

Sample code to demonstrate the problem (see the output window):

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

      

+3


source to share


1 answer


It looks like yours Border

is inside RootGrid

. Thus, every time it is clicked Border tap event

, the LostFocus

Event will be triggered.

Better you can set the event LostFocus

to the same RootGrid

. Then it might work fine.
Try it. Thank.



+1


source







All Articles