Bug 55245 - Entry Focus Problem
Summary: Entry Focus Problem
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-04-16 18:24 UTC by Hakan
Modified: 2018-02-24 13:29 UTC (History)
6 users (show)

Tags: ios uwp entry focus keyboard ac
Is this bug a regression?: No
Last known good build:


Attachments
repro project (296.99 KB, application/x-zip-compressed)
2017-04-21 18:31 UTC, Jimmy [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 55245 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Hakan 2017-04-16 18:24:10 UTC
When entry.Focus() is used in a tap event handler, it works in Android but fails in iOS. Here is a sample xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.TapTest">

    <StackLayout>

        <Entry Keyboard="Numeric" x:Name="entry" IsVisible="False" Text=""></Entry>
        <BoxView BackgroundColor="Blue">
            <BoxView.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped"></TapGestureRecognizer>
            </BoxView.GestureRecognizers>

        </BoxView>


    </StackLayout>
</ContentPage>

Here is App.xaml.cs:

MainPage = new NavigationPage(new App.TapTest());

Here is TapTest.xaml.cs

        private void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
        {
            entry.Focus();
        }

But when I use the work-araounds below, it somehow starts to work:

if (codeEntry.IsFocused == false)
{
    codeEntry.Focus();
}

OR:

await Task.Delay(1);
codeEntry.Focus();
 
OR:
Device.BeginInvokeOnMainThread(()=>entry.Focus());

I do not actually understand why these work. I discovered them by trial and error.
Comment 1 Jimmy [MSFT] 2017-04-21 18:31:08 UTC
Created attachment 21704 [details]
repro project

I'm attaching a project the reproduces the issue and confirming the report so the team can investigate further. 

This seems to be happening on iOS and UWP, but it's unclear if this is expected behavior on these platforms as the issue does not happen if the Entry is visible instead of invisible like in the repro project. The workarounds mentioned in the description also only work for iOS, not UWP.


### Regression Test
2.3.5-pre1  BAD
2.3.4.231   BAD
2.3.3.193   BAD
Comment 2 Alma Jensen 2017-08-23 20:38:59 UTC
Calling Focus() for me works on iOS using the latest release version of Xamarin Forms 2.3.4.267.  What I'm running into now is that if I open a page that Focus() got called on for an entry, and then leave that page, and then re-enter that same page or any other page with an Entry even if I tap on the Entry it cannot get focus again.
Comment 3 summer 2018-02-01 13:51:17 UTC
I am also getting this behavior

I added this to work around:

await Task.Delay(1);
codeEntry.Focus();
Comment 4 Sean Knox 2018-02-24 13:29:04 UTC
This behaviour is still present in UWP in Xamarin 2.5.0.280555.

I have for the time being applied the work around above