0

I am usingand I need the user to be able to do both tap which will open popup and scroll down. However now I am able to do only one of these two activites as seems to be unresponsive to Tapgesture.

I implemented this solution not possible and subsituted however now I cannot scroll because the is funny enough too responsive so I get pop up straight away.

            _webView.Touch += (object sender, TouchEventArgs eventArgs) => {


                if (eventArgs.Event.Action == Android.Views.MotionEventActions.Down)
                {
                    var webview = Element as ExtendedWebView;
                    
                    webview.OnTouched();
                }
            };
        }

    }

 <Grid x:Name="scroll">
                                  
                                        <controls:ExtendedWebView x:Name="czExplanation" Source="{Binding CZ, Mode=OneWay}" Touched="PopUp">

                                        </controls:ExtendedWebView>
                                   

                                </Grid>

  public ExtendedWebView()
        {
        }

        public event EventHandler Touched;

        public void OnTouched() =>
        Touched?.Invoke(this, null);
        public ICommand PannedCommand
        {
            set { SetValue(PannedCommandProperty, value); }
            get { return (ICommand)GetValue(PannedCommandProperty); }
        }
        public static readonly BindableProperty PannedCommandProperty = BindableProperty.Create(nameof(PannedCommand), typeof(ICommand), typeof(ExtendedWebView));

Update

<controls:ExtendedWebView x:Name="webView" Source="{Binding CZ, Mode=OneWay}" PannedCommand="{Binding PanCommand}" Touched="webView_Touched">
                                            </controls:ExtendedWebView>

 public class ExtendedWebViewRenderer : WebViewRenderer
    {
        public static int _webViewHeight;
        static ExtendedWebView _xwebView = null;
        public WebView _webView;
        bool isScroll;

        public ExtendedWebViewRenderer(Context context) : base(context)
        {
        }

        class ExtendedWebViewClient : WebViewClient
        {

            WebView _webView;



            public async override void OnPageFinished(WebView view, string url)
            {
                try
                {
                    _webView = view;
                    if (_xwebView != null)
                    {

                        view.Settings.JavaScriptEnabled = true;
                        await Task.Delay(100);
                        string result = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()");
                        _xwebView.HeightRequest = Convert.ToDouble(result);


                    }
                    base.OnPageFinished(view, url);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                }
            }
            public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
            {
                return true;
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            _xwebView = e.NewElement as ExtendedWebView;
            _webView = Control;

            if (e.OldElement == null)
            {
                _webView.SetWebViewClient(new ExtendedWebViewClient());
            }

            _webView.Touch += (object sender, TouchEventArgs eventArgs) =>
            {


                switch (e.Event.Action)
                {

         
    }
0

1 Answer 1

0

I checked your sample , you could improve it like following .

in xaml

Now you don't need to use BoxView any more .

<extendedtest:ExtendedWebView x:Name="webView" PannedCommand="{Binding PanCommand}" Touched="webView_Touched"/>
               

in Custom Renderer

 bool isScroll;
 _webView.Touch += _webView_Touch;
private void _webView_Touch(object sender, TouchEventArgs e)
        {

            var webview = Element as ExtendedWebView;

            bool isMove=true;

            Console.WriteLine(e.Event.Action);

            switch (e.Event.Action)
            {

                case Android.Views.MotionEventActions.Down:

                    isScroll = false;
                    break;

                case Android.Views.MotionEventActions.Move:
                   
                    isScroll = true;

                    if(isMove)
                    {
                        isMove = false;
                        webview.PannedCommand?.Execute(null);
                        
                    }

                    break;


                case Android.Views.MotionEventActions.Up:
                    
                    if(!isScroll)
                    {
                        webview.OnTouched();
                    }
                    
                    
                    break;

                default:

                    break;

            }

in ContentPage

public partial class MainPage : ContentPage
    {
    

       

        public MainPage()
        {
            InitializeComponent();

            var htmlSource = new HtmlWebViewSource();
            htmlSource.Html = @"<html><body>
  <h1>Xamarin.Forms</h1>
  <p>Welcome to WebView.</p>
 <p>Welcome to WebView.</p> <p>Welcome to WebView.</p> 
<p>Welcome to WebView.</p> <p>Welcome to WebView.</p> <p>Welcome to WebView.</p> 
<p>Welcome to WebView.</p> <p>Welcome to WebView.</p>
 <p>Welcome to WebView.</p>
  </body></html>";
            

        }

        

        private void webView_Touched(object sender, EventArgs e)
        {
            Navigation.PushAsync(new secondPage());
        }

        
    }
9
  • Don't forget to accept my answer if it helps you :) Jul 2, 2020 at 8:55
  • i am getting an error that webView_Touched was not found with correct signature Jul 2, 2020 at 9:01
  • Jesus :D ok, the scroll doesnt work but i guess i have to debug the render why it doesnt work Jul 2, 2020 at 9:59
  • You add tap , pan and scroll in the webview at same time . It might be conflict with each other . Jul 2, 2020 at 10:04
  • i am not adding tap anymore, just what you suggested <controls:ExtendedWebView x:Name="webView" Source="{Binding CZ, Mode=OneWay}" PannedCommand="{Binding PannedCommand}" Touched="webView_Touched"> </controls:ExtendedWebView> Jul 2, 2020 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.