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

Cannot scroll webpage in WebView element on Xamarin Shell #5205

Closed
Soul-Master opened this issue Feb 11, 2019 · 18 comments
Closed

Cannot scroll webpage in WebView element on Xamarin Shell #5205

Soul-Master opened this issue Feb 11, 2019 · 18 comments
Assignees

Comments

@Soul-Master
Copy link

Soul-Master commented Feb 11, 2019

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: Xamarin.Forms.Shell Spec #2415
@pauldipietro
Copy link
Contributor

Reproducible by modifying a Shell template in VS2019 preview.

@jannikscho
Copy link

jannikscho commented Mar 9, 2019

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.

@g4mb10r
Copy link

g4mb10r commented Mar 24, 2019

I have this issue as well. Please fix.

@philousb
Copy link

Hello,

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

@jannikscho
Copy link

The problem is still there. Tested with preview 9.

@samhouts samhouts added the i/low Has trivial workaround; affects very few users label May 4, 2019
@samhouts
Copy link
Member

samhouts commented May 4, 2019

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);
		}

@philousb
Copy link

philousb commented May 18, 2019

It works for me.
thx.

@wagenheimer
Copy link

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?

@philousb
Copy link

philousb commented Jul 2, 2019

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

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

namespace MyNamespace
{
    public class MyWebView : WebView
    {
    }
}
  1. For each project create a custom renderer
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);
        }
    }
}

@dgerding
Copy link

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 :) ?

@samhouts
Copy link
Member

#6310

@MSiccDev
Copy link

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

@samhouts
Copy link
Member

closed by #7032

@jonhudson12
Copy link

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

@blndr83
Copy link

blndr83 commented May 31, 2020

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

@MauriceMatekCode
Copy link

Hey there - my name is Maurice.

Weirdly this whole thing doesn't seem to work for me.
I use the pdfjs-Library to display a local pdf-file in a WebView on Android. I am using Xamarin-Forms-Shell.

I am also using an instance of WebView with a CustomRenderer linked to it as mentioned in this thread before. Inside the Custom Renderer for Android I have the following code to fix the scrolling bug:

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

the problem is: scrolling still does not work. Sometimes although, when using two fingers and scrolling fast it zooms a little and scrolls a bit.

I really don't know what I could do about that. Which is quite frustrating.

Thanks for any help
Maurice

@MauriceMatekCode
Copy link

Hey there - my name is Maurice.

Weirdly this whole thing doesn't seem to work for me.
I use the pdfjs-Library to display a local pdf-file in a WebView on Android. I am using Xamarin-Forms-Shell.

I am also using an instance of WebView with a CustomRenderer linked to it as mentioned in this thread before. Inside the Custom Renderer for Android I have the following code to fix the scrolling bug:

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

the problem is: scrolling still does not work. Sometimes although, when using two fingers and scrolling fast it zooms a little and scrolls a bit.

I really don't know what I could do about that. Which is quite frustrating.

Thanks for any help
Maurice

I was able to fix the issue by using Xamarin.Forms 5.0 and the newest prerelease of pdfjs.

Thanks anyway ^^

@scott-hooper-hordernit
Copy link

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

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

namespace MyNamespace
{
    public class MyWebView : WebView
    {
    }
}
  1. For each project create a custom renderer
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);
        }
    }
}

Did you have an iOS version of this custom renderer? I don't think iOS handles touch events like this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests