Windows Phone 8.1 XAML application. How to prevent incorrect auto focus change?

Imagine the following simple page layout:

<Page x:Class="AutoFocusBug.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <!--<Button x:Name="FocusHolder" Width="0" Height="0" MinHeight="0" MinWidth="0"/>--> <TextBox Text="zxczczczczx"/> <Button x:Name="Button1" Content="Button1" Click="ButtonBase_OnClick"/> </StackPanel> </Page> 

and the following code:

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { Button1.Visibility = Visibility.Collapsed; } 

When I press Button1 , it hides (which is expected and required), but for some reason the TextBox automatically gets focus and the on-screen keyboard appears. I do not want this unexpected autofocus.

I can try to do something with focus in the Click handler, but in a real application it is implemented using ViewModel with commands, etc., and it looks like a dirty hack (and also the keyboard appears for a short time, even if I change immediately focus after hiding the button).

The second approach I found was to create an “invisible” button somewhere in front of the text box (commented by FocusHolder ), which is pretty good, but also not like the right technique.

So what is this? Is this some mechanism that I can somehow adjust to “redirect” focus from a collapsed element? Or is this a mistake? What is the correct way to prevent this unwanted autofocus?

+6
source share
3 answers

The reason for this is because the Tab index switches from a button to a text field. This is very annoying to WP8.1. I have not found a “good” solution for this, but it may be cleaner than your current solution.

xaml

 <TextBox x:Name="myTextBox" Text="zxczczczczx"/> 

Function

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { myTextBox.IsTabStop = false; Button1.Visibility = Visibility.Collapsed; myTextBox.IsTabStop = true; } 

Good luck.

+4
source

I was able to solve this problem by installing TabIndex.

Set the index order to focus on your second control in front of the TextBox.

 <Page x:Class="AutoFocusBug.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <!--<Button x:Name="FocusHolder" Width="0" Height="0" MinHeight="0" MinWidth="0"/>--> <TextBox Text="zxczczczczx" TabIndex="2"/> <Button x:Name="Button1" Content="Button1" Click="ButtonBase_OnClick" TabIndex="1"/> </StackPanel> 

+4
source

Try setting TabNavigation = "Loop" to the button. This works for me.

0
source

Source: https://habr.com/ru/post/980522/


All Articles