尝试聚焦时 Webview 文本字段滚动到顶部 - Xamarin.forms iOS

Webview text field gets scrolled to top when trying to focus - Xamarin.forms iOS

我有 xamarin 表单 iOS 应用程序包含一个 webview。 Webview 加载 html 页面包含一些文本 fields.The 我面临的问题是每当我们单击文本字段时,键盘显示并且整个网页将滚动到顶部并且文本字段将变为 invisible.When 我们在浏览器上打开同一个页面,它工作正常。我已将 xam.plugins.forms.keyboard overlap 用于我的其他非 webview 页面。我该如何解决这个问题?我希望当我们关注它时,textview 字段应该在那里。

我在这个 上看到过类似的问题。但是并没有解决我的问题。感谢任何帮助。

我的XAML

 <ContentPage.Content>
        <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <WebView x:Name="Webview"  
             HeightRequest="1000"  
             WidthRequest="1000"  
             IsVisible="False"
             Navigating="OnNavigating"  
             Navigated="OnNavigated"  
             VerticalOptions="FillAndExpand"/>
        </Grid>
    </ContentPage.Content>

您可以把插件去掉,自己实现。

例如,您可以选中 source code 并将其作为 PageRenderer 复制到您的项目中。

在页面渲染器中,我添加了一个bool值来检查你是否需要使用这个渲染器。这里我用ClassId来查:

bool useThisRenderer;

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);

    if (e.NewElement != null)
    {
        Page p = e.NewElement as Page;

        if (p.ClassId == "0")
        {
            useThisRenderer = false;
        }
        else
        {
            useThisRenderer = true;
        }
    }
    
}

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    if (useThisRenderer)
    {
        var page = Element as ContentPage;

        if (page != null)
        {
            var contentScrollView = page.Content as ScrollView;

            if (contentScrollView != null)
                return;

            RegisterForKeyboardNotifications();
        }
    }
}

public override void ViewWillDisappear(bool animated)
{
    base.ViewWillDisappear(animated);

    if (useThisRenderer)
    {
        UnregisterForKeyboardNotifications();
    }
}

然后在你的 Xamarin.forms 项目中,如果你的内容页面之一不需要这个渲染器(例如当前页面你有一个 webview),你可以设置 classid = "0" 来避免这种情况:

public MainPage()
{
    InitializeComponent();

    this.ClassId = "0";
}

如果你需要这个渲染器,那么什么都不做this.ClassId。

我上传了一个示例项目here,有任何问题都可以问我。