Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Bug] Calling Focus() on a WebView on UWP does not move the focus to the WebView #12497

Open
@johnshardman

Description

@johnshardman

Programmatically setting the Focus to a WebView on UWP using XF 4.8 does not work.

Native UWP does support setting Focus to a WebView, but attempting to do the same using Xamarin.Forms 4.8 fails.
Documentation of the UWP Focus API can be found at https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.webview.focus?view=winrt-19041

To demonstrate the problem, use the following code. Push an instance of TestWebViewTabIndexPageView onto the NavigationStack. Pressing the third Button should result in the focus moving to the WebView. It doesn't.

using Xamarin.Forms;

using System.Threading.Tasks;
using System;

namespace ViewsUsingXamarinForms
{
public class TestWebViewTabIndexPageView : ContentPage
{
private bool _firstTimeOnAppearing = true;

    public TestWebViewTabIndexPageView()
    {
        PopulatePage();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        if (_firstTimeOnAppearing)
        {
            _firstTimeOnAppearing = false;

            //PopulatePage();

            // The following is just here as a convenience.
            // It's a nasty, hacky way of setting the initial focus, 
            // that introduces a race condition. Good enough for
            // this repro sample though.
            Task.Run(async () =>
            {
                await Task.Delay(1000);
                Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                {
                    if (_button1 != null)
                        _button1.Focus();
                });
            });
        }
    }

    private Button _button1;
    private Button _button2;
    private Button _button3;
    private WebView _webView;

    private void PopulatePage()
    {
        int defaultTabIndex = Int32.MaxValue; // UWP says this should be the max int. Xamarin.Forms docs say it should be 0;

        _button1 = new Button
        {
            Text = "Button 1 (should be first in tab order)",
            TextColor = Color.Black,
            BackgroundColor = Color.White,
            TabIndex = defaultTabIndex,
            VerticalOptions = LayoutOptions.Start
        };

        _webView = new WebView
        {
            BackgroundColor = Color.White,
            HorizontalOptions = LayoutOptions.Fill,
            TabIndex = defaultTabIndex,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Source = new HtmlWebViewSource
            {
                Html = "<html><body>Hello (should be second in tab order)<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29</body></html>"
            }
        };

        _button2 = new Button
        {
            Text = "Button 2 (should be third in tab order)",
            TextColor = Color.Black,
            BackgroundColor = Color.White,
            TabIndex = defaultTabIndex,
            VerticalOptions = LayoutOptions.End
        };

        _button3 = new Button
        {
            Text = "Button 3 (press to set focus to WebView)",
            TextColor = Color.Black,
            BackgroundColor = Color.White,
            TabIndex = defaultTabIndex,
            VerticalOptions = LayoutOptions.End,
            Command = new Command(() => 
            {
                _webView.Focus(); // _button1.Focus();
            })
        };

        Content = new StackLayout
        {
            HorizontalOptions = LayoutOptions.Fill,
            VerticalOptions = LayoutOptions.Fill,
            BackgroundColor = Color.Pink,
            Children =
            {
                _button1,
                _webView,
                _button2,
                _button3
            }
        };
    }
}

}

Basic Information

  • Version with issue: 4.8

There is a generic Focus issue already logged at #5616
but I thought it worth raising this one separately, as this one should be easy to fix. The underlying platform supports putting the Focus onto a WebView. It's just a case of hooking it up.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions