单击文本框时,带有表单的 Xamarin Webview 会滚动出视图 [英] Xamarin Webview with form scrolls out of view when clicking textbox

查看:24
本文介绍了单击文本框时,带有表单的 Xamarin Webview 会滚动出视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用webview加载一个简单的html表单,表单显示正常.单击输入字段时,整个表单消失,在显示屏底部显示向上和向下箭头和完成按钮.这种行为不会发生在普通的 Chrome 浏览器中.有什么想法吗?

最好的问候安德烈亚斯

XML:

 

HTML:

 <script type="text/javascript">//填写您的可发布密钥Stripe.setPublishableKey('pk_test_1GitP47uZiwo4PKrDDSo8P3X');var stripeResponseHandler = 函数(状态,响应){var $form = $('#payment-form');如果(响应.错误){//在表单上显示错误$form.find('.payment-errors').text(response.error.message);$form.find('button').prop('disabled', false);} 别的 {//token 包含 id、last4 和卡片类型var token = response.id;//将令牌插入到表单中,以便将其提交给服务器$form.append($('<input type="hidden" name="stripeToken"/>').val(token));//并重新提交$form.get(0).submit();}};jQuery(函数($){$('#payment-form').submit(function(e) {var $form = $(this);//禁用提交按钮以防止重复点击$form.find('button').prop('disabled', true);Stripe.card.createToken($form, stripeResponseHandler);//防止表单使用默认操作提交返回假;});});<身体><form action="/your-charge-code" method="POST" id="payment-form"><span class="payment-errors"></span><div class="form-row"><标签><span>卡号</span><input type="text" size="20" data-stripe="number">

<div class="form-row"><标签><span>到期 (MM/YY)</span><input type="text" size="2" data-stripe="exp_month"><跨度>/</span><input type="text" size="2" data-stripe="exp_year">

<div class="form-row"><标签><span>CVC</span><input type="text" size="4" data-stripe="cvc">

<div class="form-row"><标签><span>帐单邮政编码</span><input type="text" size="6" data-stripe="address_zip">

<input type="submit" class="submit" value="提交付款"></表单>

解决方案

奇怪,所以我使用您的 HTML 代码制作了一个网页,然后创建了一个带有 WebView 指向的新 Xamarin.Forms 解决方案到我从您的 HTML 创建的页面.当我单击任何输入字段时,软键盘按预期显示.但是,如果我确定 WebView 位于屏幕底部,在键盘显示的下方,键盘会隐藏 WebView.要解决此问题,您需要一个自定义页面渲染器 [1] 在键盘出现时向上滚动视图,并在键盘消失时向下滚动视图.

这里有一些示例代码可以做到这一点.我假设您只想在具有 WebView 的页面上执行此操作,因此首先在 Forms PCL 项目中创建一个名为 WebViewContentPageContentPage 空子类代码>:

公共类 WebViewContentPage : ContentPage {}

然后您从具有 WebView 的实际页面的 WebViewContentPage 继承,在我的测试中我将其称为 TestWebInputPage:

公共部分类 TestWebInputPage : WebViewContentPage{公共 TestWebInputPage(){初始化组件();}}

我的 Xaml 是(将 UrlToWebPageHere 更改为您网页的实际网址):

<堆栈布局><标签文字=占用空间"HeightRequest="400"/><网页视图x:Name="webView"导航="Handle_Navigating"来源="UrlToWebPageHere"VerticalOptions="FillAndExpand"/></StackLayout></local:WebViewContentPage>

最后是自定义页面渲染器代码.这在 iOS 应用项目中:

使用系统;使用 Xamarin.Forms;使用 TestWebInput.iOS;使用 Xamarin.Forms.Platform.iOS;使用基金会;使用 UIKit;使用 TestWebInput;[组装:ExportRenderer(typeof(WebViewContentPage), typeof(ContentPageRenderer))]命名空间 TestWebInput.iOS{公共类 ContentPageRenderer : PageRenderer{私人 NSObject keyBoardWillShow;私人 NSObject keyBoardWillHide;私人 nfloat scrollAmout;私人双动画持续时间;私有 UIViewAnimationCurve 动画曲线;私人布尔键盘显示;公共覆盖无效 ViewDidLoad(){base.ViewDidLoad();keyBoardWillShow = UIKeyboard.Notifications.ObserveWillShow(KeyboardWillShow);keyBoardWillHide = UIKeyboard.Notifications.ObserveWillHide(KeyboardWillHide);}void KeyboardWillShow(对象发送者,UIKeyboardEventArgs args){如果(!键盘显示){键盘显示 = 真;animDuration = args.AnimationDuration;动画曲线 = args.AnimationCurve;var r = UIKeyboard.FrameBeginFromNotification(args.Notification);scrollAmout = r.Height;滚动视图(真);}}void KeyboardWillHide(对象发送者,UIKeyboardEventArgs args){如果(键盘显示){键盘显示 = 假;animDuration = args.AnimationDuration;动画曲线 = args.AnimationCurve;var r = UIKeyboard.FrameBeginFromNotification(args.Notification);scrollAmout = r.Height;滚动视图(假);}}私人无效ScrollTheView(布尔规模){UIView.BeginAnimations(string.Empty, IntPtr.Zero);UIView.SetAnimationDuration(animDuration);UIView.SetAnimationCurve(animCurve);var frame = View.Frame;如果(规模)frame.Y -= scrollAmout;别的frame.Y += scrollAmout;View.Frame = 框架;UIView.CommitAnimations();}}}

由于此渲染器实际上会在键盘显示和隐藏时上下滚动整个本机页面,因此在 Forms Xaml 中如何布局页面应该无关紧要.重要的是您的表单页面继承自 WebViewContentPage.

我希望这会有所帮助!

[1] https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/contentpage/

Using a webview to load a simple html form, the form displays ok. When clicking an input field the entire form disappears showing arrows up and down and done button at bottom part of display. This behavior does not happen in a normal Chrome browser. Any idéas?

Best regards Andreas

Xaml:

            <WebView Grid.Row="0" IsEnabled="false" 
                    x:Name="webview"  
                Navigating="webViewNavigating"></WebView>

        </Grid>
    </ContentPage.Content>
        <ContentPage.ToolbarItems>
    <ToolbarItem Text="Byt Betalsätt" Activated="BtnBack_Clicked" Priority="0" Order="Primary" />
</ContentPage.ToolbarItems>

</ContentPage>

HTML:

  <!-- New section -->
  <script type="text/javascript">
    // Fill in your publishable key
    Stripe.setPublishableKey('pk_test_1GitP47uZiwo4PKrDDSo8P3X');

    var stripeResponseHandler = function(status, response) {
      var $form = $('#payment-form');

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // token contains id, last4, and card type
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));
        // and re-submit
        $form.get(0).submit();
      }
    };

    jQuery(function($) {
      $('#payment-form').submit(function(e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.card.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });
    });
  </script>
</head>

<body>
<form action="/your-charge-code" method="POST" id="payment-form">
  <span class="payment-errors"></span>

  <div class="form-row">
    <label>
      <span>Card Number</span>
      <input type="text" size="20" data-stripe="number">
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Expiration (MM/YY)</span>
      <input type="text" size="2" data-stripe="exp_month">
    </label>
    <span> / </span>
    <input type="text" size="2" data-stripe="exp_year">
  </div>

  <div class="form-row">
    <label>
      <span>CVC</span>
      <input type="text" size="4" data-stripe="cvc">
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Billing Postal Code</span>
      <input type="text" size="6" data-stripe="address_zip">
    </label>
  </div>

  <input type="submit" class="submit" value="Submit Payment">
</form>

</body>

解决方案

Odd, so I made a web page using your HTML code, and then created a new Xamarin.Forms solution with a WebView pointing to the page I created from your HTML. When I clicked any of the input fields the soft keyboard showed as expected. However if I made sure the WebView was at the bottom of the screen, under where the keyboard shows, the keyboard does hide the WebView. To fix this, you would need a custom page renderer [1] to scroll the view up when the keyboard shows up and scroll the view back down when the keyboard goes away.

Here is some sample code to do this. I assume you would only want to do this on pages that have that WebView, so first in the Forms PCL project create an empty subclass of ContentPage called WebViewContentPage:

public class WebViewContentPage : ContentPage {}

Then you inherit from the WebViewContentPage for the actual page that has the WebView, in my test I called it TestWebInputPage:

public partial class TestWebInputPage : WebViewContentPage
{ 
    public TestWebInputPage()
    {
        InitializeComponent();
    }
}

My Xaml is (change UrlToWebPageHere to the actual url of your web page):

<?xml version="1.0" encoding="utf-8"?>
<local:WebViewContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestWebInput" x:Class="TestWebInput.TestWebInputPage">
    <StackLayout>
        <Label 
            Text="Take up space" 
            HeightRequest="400" />
        <WebView 
            x:Name="webView" 
            Navigating="Handle_Navigating" 
            Source="UrlToWebPageHere" 
            VerticalOptions="FillAndExpand" />
    </StackLayout>
</local:WebViewContentPage>

And finally the custom page renderer code. This goes in the iOS app project:

using System;
using Xamarin.Forms;
using TestWebInput.iOS;
using Xamarin.Forms.Platform.iOS;
using Foundation;
using UIKit;
using TestWebInput;

[assembly: ExportRenderer(typeof(WebViewContentPage), typeof(ContentPageRenderer))]
namespace TestWebInput.iOS
{
    public class ContentPageRenderer : PageRenderer
    {   
        private NSObject keyBoardWillShow;
        private NSObject keyBoardWillHide;
        private nfloat scrollAmout;
        private double animDuration;
        private UIViewAnimationCurve animCurve;
        private bool keyboardShowing;

        public override void ViewDidLoad()
        {

            base.ViewDidLoad();

            keyBoardWillShow = UIKeyboard.Notifications.ObserveWillShow(KeyboardWillShow);

            keyBoardWillHide = UIKeyboard.Notifications.ObserveWillHide(KeyboardWillHide);
        }

        void KeyboardWillShow(object sender, UIKeyboardEventArgs args)
        {
            if (!keyboardShowing)
            {
                keyboardShowing = true;
                animDuration = args.AnimationDuration;
                animCurve = args.AnimationCurve;

                var r = UIKeyboard.FrameBeginFromNotification(args.Notification);
                scrollAmout = r.Height;
                ScrollTheView(true);
            }
        }

        void KeyboardWillHide(object sender, UIKeyboardEventArgs args)
        {
            if (keyboardShowing)
            {
                keyboardShowing = false;
                animDuration = args.AnimationDuration;
                animCurve = args.AnimationCurve;

                var r = UIKeyboard.FrameBeginFromNotification(args.Notification);
                scrollAmout = r.Height;
                ScrollTheView(false);
            }
        }

        private void ScrollTheView(bool scale)
        {
            UIView.BeginAnimations(string.Empty, IntPtr.Zero);
            UIView.SetAnimationDuration(animDuration);
            UIView.SetAnimationCurve(animCurve);

            var frame = View.Frame;

            if (scale)
                frame.Y -= scrollAmout;
            else
                frame.Y += scrollAmout;
            View.Frame = frame;
            UIView.CommitAnimations();
        }
    }
}

Since this renderer actually scrolls the entire native page up and back down when the keyboard shows and hides, it should not matter how you layout the page in the Forms Xaml. All that matters is that your Forms page inherits from WebViewContentPage.

I hope this helps!

[1] https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/contentpage/

这篇关于单击文本框时,带有表单的 Xamarin Webview 会滚动出视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆