Xamarin.forms: Cannot scroll webpage in WebView element on Xamarin Shell

Created on 11 Feb 2019  路  15Comments  路  Source: xamarin/Xamarin.Forms

Description

Steps to Reproduce

  1. Pull Gastropods project from Github
    https://github.com/davidortinau/Gastropods.git
  2. Update Xamarin Form to latest prerelease version ()
  3. Replace content of MainPage.xaml page with single WebView element

<WebView Source="https://www.youtube.com/" ></WebView>

  1. Build & Debug
  2. After loaded, try to scroll YouTube page

Expected Behavior

YouTube webpage should be scrolled.

Actual Behavior

Nothing happened after scrolling.

Basic Information

  • Version with issue: 4.0.0.135214-pre4, 4.0.0.8055-pre1
  • Last known good version: -
  • IDE: VS 2017 15.9.6
  • Platform Target Frameworks:

    • Android: Android Pie (9.0)

  • Nuget Packages:
  • Affected Devices: Samsung S7 Edge (Android 8.0)
    Parent: #2415
gestures 馃枛 shell webview 4 low Android bug

Most helpful comment

You have to create a custom renderer for the webview (Android, iOS)

  1. create a custom webview
    ```C#
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Xamarin.Forms;

namespace MyNamespace
{
public class MyWebView : WebView
{
}
}


2. For each project create a custom renderer
```C#
using Android.Content;
using Android.Views;

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;

using MyNamespace;
using MyNamespace.Droid;

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace MyNamespace.Droid
{
    class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
        }

        public override bool DispatchTouchEvent(MotionEvent e)
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            return base.DispatchTouchEvent(e);
        }
    }
}

All 15 comments

Reproducible by modifying a Shell template in VS2019 preview.

Hello,

The problem also occurs in my project with WebView and Maps (Xamarin.Forms.GoogleMaps).
I could solve it easily by a custom ContentView.

See below...

Shared-Project:

Create a custom ContentView:
public class GesturelessContentView : ContentView { }

In Xaml add the WebView to this ContentView:

<controls:GesturelessContentView x:Name="view" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <WebView x:Name="webView" Source="{Binding Source}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/> </controls:GesturelessContentView>

Android-Project:
Create a custom ViewRenderer for this ContentView and override the "DispatchTouchEvent":

public override bool DispatchTouchEvent(MotionEvent e) { /* Switching between touch-events switch (e.Action) { case MotionEventActions.Down: case MotionEventActions.Move: case MotionEventActions.Up: this.Parent.RequestDisallowInterceptTouchEvent(true); break; }*/ // For all touch-events this.Parent.RequestDisallowInterceptTouchEvent(true); return base.DispatchTouchEvent(e); }

In my case it works as expected.

I have this issue as well. Please fix.

Hello,

Having the same issue in 4.0.0 pre 9. Will this be fixed soon ?

The problem is still there. Tested with preview 9.

As a workaround, you can create a custom renderer for your WebView and add the following override:

        public override bool DispatchTouchEvent(MotionEvent e)
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            return base.DispatchTouchEvent(e);
        }

It works for me.
thx.

As a workaround, you can create a custom renderer for your WebView and add the following override:

      public override bool DispatchTouchEvent(MotionEvent e)
      {
          Parent.RequestDisallowInterceptTouchEvent(true);
          return base.DispatchTouchEvent(e);
      }

Could you please explain how can I do this?

You have to create a custom renderer for the webview (Android, iOS)

  1. create a custom webview
    ```C#
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Xamarin.Forms;

namespace MyNamespace
{
public class MyWebView : WebView
{
}
}


2. For each project create a custom renderer
```C#
using Android.Content;
using Android.Views;

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;

using MyNamespace;
using MyNamespace.Droid;

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace MyNamespace.Droid
{
    class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
        }

        public override bool DispatchTouchEvent(MotionEvent e)
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            return base.DispatchTouchEvent(e);
        }
    }
}

This is still not working for me. I get the same "no scrolling" behavior in standard WebView and when using Xam.Plugin.Web. This seems like a pretty big bug since webviews are common. Help :) ?

6310

I can confirm that the workaround works (at least for Android). Even though I would prefer it to work out of the box...

closed by #7032

@philousb - you sir deserve a medal. Works perfect. Ty!

I have tried philousb custom webview renderer on a contentpage with a scrollview inside the scrollview a grid and in the grid at the 3 Row a webview. Without the custom renderer scrolling works only in landscape mode. With the custom renderer it works only when it is not in landscape mode. When i remove the scrollview it works with standard webview in both modes. Tested on a Huawei p20 pro with android 9

Was this page helpful?
0 / 5 - 0 ratings