'Why changes the focus to the entry when I click the label? Xamarin.Forms/UWP
I have a problem and I made an example to show it.
This code establishes two pairs of label/entry. If I have the focus set to the 2nd entry and I click the 2nd label, the focus goes to the 1st entry (!?)...
I need the ScrollView because in the real app, there are a lot of views that need to be scrolled.
I tried this in a UWP project on a Win10/11 PC.
How can this strange behavior be avoided?
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestForm.MainPage">
<ScrollView x:Name="scroll">
<StackLayout>
<Label Text="Title01"/>
<Entry/>
<Label Text="Title02"/>
<Entry/>
</StackLayout>
</ScrollView>
</ContentPage>
Solution 1:[1]
As a workaround, you can add the TapGestureRecognizer on stacklayout to set unfous on first entry like:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestForm.MainPage">
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnTapGestureRecognizerTapped"/>
</StackLayout.GestureRecognizers>
<ScrollView x:Name="scroll">
<StackLayout>
<Button HeightRequest="0" WidthRequest="1" />//add button
<Label Text="Title01"/>
<Entry x:Name="entry1"/>
<Label Text="Title02"/>
<Entry/>
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage>
code behind:
void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
if(!entry1.isFocused)
{entry1.Unfocus(); }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
