Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

xamarin.forms - not possible and subsituted

i have a webview and i have tried to use tap gesture however it was unresponsive therefore i have decided to use box view around the webview which works fine and i am able to scroll and tap the box view. However once i scroll on the bottom and the following content is not that long to be scrolled i am still at the end of the vebview and have to scroll to the top to actually view the content. Its hard to explain but basically if i have very long sentence i can scroll downr secons sentence is only few words and i have to scroll to the top. I need that to be done automatically. Do you have any experience with this?

[assembly: ExportRenderer(typeof(ExtendedWebView), typeof(ExtendedWebViewRenderer))]
namespace Droid.Renderers
{
    public class ExtendedWebViewRenderer : WebViewRenderer
    {
        public static int _webViewHeight;
        static ExtendedWebView _xwebView = null;
        WebView _webView;

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

                        view.Settings.JavaScriptEnabled = 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());
            }

        }
    }
}

Shared project

 public class ExtendedWebView : WebView
    {

        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));
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had to ad boxview so i can tap the webview

Actually , it is necessary to use BoxView . The best way is to use CustomRenderer to add a TapGestureRecognizer and let webview recognize it.

in Forms

using System;
using Xamarin.Forms;
namespace xxx
{
    public class MyWebView:WebView
    {
        public MyWebView()
        {
        }

        public event EventHandler Touched;

        public void OnTouched() =>
        Touched?.Invoke(this, null);
    }
}

in Android

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

        }

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

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


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

                    webview.OnTouched();
                }



            };
        }

    }
}

in iOS

using Foundation;
using UIKit;
using xxx;
using xxx.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using ObjCRuntime;
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace xxx.iOS
{
    public class MyWebViewRenderer:WkWebViewRenderer, IUIGestureRecognizerDelegate
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (NativeView != null)
            {
                UITapGestureRecognizer tapGestureRecognizer = new UITapGestureRecognizer(this, new Selector("HandleTap:"))
                {
                    WeakDelegate = this,
                    CancelsTouchesInView = false
                };
                NativeView.AddGestureRecognizer(tapGestureRecognizer);
            }

        }


        [Export("gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:")]
        public bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
        {
            return true;
        }

        [Export("HandleTap:")]
        void HandleTap(UITapGestureRecognizer tap)
        {
            var webview = Element as MyWebView;

            webview.OnTouched();
        }
    }
}

Now you just need to use it like

<local:MyWebView x:Name="browser"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" 
           Touched = "xxx">


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...