自定义渲染如何滚动和点击 [英] Custom render how to scroll and tap

查看:46
本文介绍了自定义渲染如何滚动和点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用,我需要用户能够同时执行这两个轻击,这将打开弹出窗口并向下滚动.但是,现在我只能执行这两种活动之一,因为它们似乎对Tapgesture没有反应.

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

更新

<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)
                {

         
    }

推荐答案

我检查了您的示例,可以像下面那样改进它.

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

现在,您不再需要使用 BoxView .

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

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

在自定义渲染器中

 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;

            }

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

        
    }

这篇关于自定义渲染如何滚动和点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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