打开目标=“_blank”与 Xamarin.Forms WebView 的链接

发布于 2025-01-13 15:09:09 字数 658 浏览 2 评论 0原文

我最近遇到了 target 属性设置为 _blank 的链接问题。点击它们没有任何效果。 WebView 不加载链接页面。

搜索周围我找到了这个解决方案: https://github.com/xamarin/Xamarin.Forms /issues/12917 正在设置 SetSupportMultipleWindows(false)。更具体地说,建议是插入以下代码行:webView.Settings.SetSupportMultipleWindows(true);

尝试出现以下错误:错误 CS1061 'WebView' 不包含 'Settings' 的定义,并且找不到接受类型 'WebView' 的第一个参数的可访问扩展方法 'Settings' (您是否缺少使用指令或程序集引用?)

有没有办法克服该错误?

I have recently encountered an issue with links having the target attribute set to _blank. Tapping on them has no effect. The WebView does not load the linked page.

Searching around I found this solution: https://github.com/xamarin/Xamarin.Forms/issues/12917 which is setting SetSupportMultipleWindows(false). More specifically the suggestion is to insert the following line of code: webView.Settings.SetSupportMultipleWindows(true);.

Trying that I get the following error: Error CS1061 'WebView' does not contain a definition for 'Settings' and no accessible extension method 'Settings' accepting a first argument of type 'WebView' could be found (are you missing a using directive or an assembly reference?)

Is there a way to overcome the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2025-01-20 15:09:09

经过多次尝试错误的方向后,我找到了对我有用的代码行。在 Custom Webview Renderer 中的 OnElementChanged 方法的重写中,以下代码可以打开具有 target=_blank 属性的链接:

Control.Settings.SetSupportMultipleWindows(false);

SetSupportMultipleWindows; 应设置为false,而不是正如所建议的那样正确

After many tries towards the wrong direction I found the line of code that worked for me. In the override of the OnElementChanged method in the Custom Webview Renderer this is the code that makes the opening of links with target=_blank attribute possible:

Control.Settings.SetSupportMultipleWindows(false);

SetSupportMultipleWindows; should be set to false, not to true as it has been suggested.

笑看君怀她人 2025-01-20 15:09:09

您可以使用以下代码在混合渲染器OnElementChanged中添加对多个窗口的支持:

Control.Settings.SetSupportMultipleWindows(true);

编辑:

public class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }

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

            if(Control!=null)
            {
                Control.Settings.SetSupportMultipleWindows(true);
                Control.Settings.JavaScriptEnabled = true;
                Control.SetWebChromeClient(new MyWebChromeClient());
            }

        }

    }

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

}

You could add support for multiple windows in OnElementChanged of Hybrid renderer with code:

Control.Settings.SetSupportMultipleWindows(true);

edit:

public class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }

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

            if(Control!=null)
            {
                Control.Settings.SetSupportMultipleWindows(true);
                Control.Settings.JavaScriptEnabled = true;
                Control.SetWebChromeClient(new MyWebChromeClient());
            }

        }

    }

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文