SBoss
SBoss

Reputation: 9215

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

Some example code to demonstrate the problem (watch 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);
        }
    }
}

Upvotes: 3

Views: 396

Answers (1)

asitis
asitis

Reputation: 3031

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.

Better you can set LostFocus event on the same RootGrid. Then it may work fine.
Please try it. Thanks.

Upvotes: 1

Related Questions