不可能并被替代 [英] not possible and subsituted
问题描述
我有一个webview,我尝试使用点击手势,但是它没有响应,因此我决定在webview周围使用框视图,这样效果很好,并且我能够滚动和点击框视图.但是,一旦我在底部滚动并且以下内容滚动的时间不长,我仍处于vebview的结尾,必须滚动到顶部才能实际查看内容.很难解释,但基本上,如果我的句子很长,我可以滚动下层secons句子,只有几个字,而且我必须滚动到顶部.我需要自动完成.您对此有任何经验吗?
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());
}
}
}
}
共享项目
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));
}
推荐答案
我必须对boxview进行广告投放,这样我才能点击webview
I had to ad boxview so i can tap the webview
实际上,有必要使用BoxView.最好的方法是使用 CustomRenderer 添加 TapGestureRecognizer 并让Webview识别它.
Actually , it is necessary to use BoxView . The best way is to use CustomRenderer to add a TapGestureRecognizer and let webview recognize it.
using System;
using Xamarin.Forms;
namespace xxx
{
public class MyWebView:WebView
{
public MyWebView()
{
}
public event EventHandler Touched;
public void OnTouched() =>
Touched?.Invoke(this, null);
}
}
在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();
}
};
}
}
}
在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();
}
}
}
现在您只需要使用它
<local:MyWebView x:Name="browser"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
Touched = "xxx">
这篇关于不可能并被替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!