CS0115 'HybridWebViewRenderer.OnCreateWindow(WebView, bool, bool, Message)': 找不到合适的方法来覆盖 - Xamarin.Forms

CS0115 'HybridWebViewRenderer.OnCreateWindow(WebView, bool, bool, Message)': no suitable method found to override - Xamarin.Forms

我正在尝试在此处实施作为答案提供的解决方案:

Android 项目中 我创建了一个自定义渲染器,其中包含以下代码摘录:

[assembly: ExportRenderer(typeof(HybridWebView), typeof(MyApp.controls.HybridWebViewRenderer))]


public class HybridWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            Control.Settings.SetSupportMultipleWindows(true);
        }
      
    }

在我的共享项目中,我创建了一个HybridWebView,其中包含以下代码摘录:

public class HybridWebView : WebView
    {
       
        public override bool OnCreateWindow(Android.Webkit.WebView view, bool isDialog, bool isUserGesture, Android.OS.Message resultMsg)
        {
            Android.Webkit.WebView newWebView = new Android.Webkit.WebView(_context);
            view.AddView(newWebView);
            Android.Webkit.WebView.WebViewTransport transport = (Android.Webkit.WebView.WebViewTransport)resultMsg.Obj;
            transport.WebView = newWebView;
            resultMsg.SendToTarget();
            return true;
        }
    }

这会导致以下错误:

CS0115 'HybridWebViewRenderer.OnCreateWindow(WebView, bool, bool, Message)': no suitable method found to override

知道如何解决这个问题吗?

就我而言,我们不需要在 Target project.Also 的自定义渲染器中覆盖 OnCreateWindow,请注意 OnElementChanged 方法自定义 Webview.We 需要将 SetSupportMultipleWindows 设置为 true,启用 JS,设置 SetWebChromeClient,设置 WebViewClient,设置 AddJSInterface 最后但并非最不重要的一点,Web 视图的 LoadUrl

以下是供您参考的代码片段:

[assembly: ExportRenderer(typeof(HybridWebView), 
typeof(HybridWebViewRenderer))]
namespace AppHybridWebView.Droid
{
    public  class HybridWebViewRenderer : WebViewRenderer
    {

        public HybridWebViewRenderer(Context context) : base(context)
        {
            _context = context;
        }

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

            if (Control != null)
            {
                Control.Settings.SetSupportMultipleWindows(false);
                Control.Settings.JavaScriptEnabled = true;
                Control.SetWebChromeClient(new MyWebChromeClient());
                Control.SetWebViewClient(new JavascriptWebViewClient(this, $"javascript: {JavascriptFunction}"));
                Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                Control.LoadUrl($"file:///android_asset/Content/{((HybridWebView)Element).Uri}");//LoadUrl
            }

        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                ((HybridWebView)Element).Cleanup();
            }
            base.Dispose(disposing);
        }

    }

    public class MyWebChromeClient : WebChromeClient
    {
        public override bool OnCreateWindow(Android.Webkit.WebView view, bool isDialog, bool isUserGesture, Message resultMsg)
        {

            if (!isDialog)
            {
                return true;
            }

            return base.OnCreateWindow(view, isDialog, isUserGesture, resultMsg);
        }
    }
}

xaml中的代码:

<local:HybridWebView x:Name="hybridWebView" Uri="index.html" />