首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >允许弹出窗口在Xamarin窗体WebView中工作

允许弹出窗口在Xamarin窗体WebView中工作
EN

Stack Overflow用户
提问于 2021-06-09 11:18:37
回答 1查看 416关注 0票数 0

这似乎是一件简单的事情-我想使用WebView从第三方加载/运行一些JavaScript。

当我加载页面时,我会收到一条消息:“您必须允许弹出窗口才能工作”。我想我需要创建一个定制的WebView ( iOS和Android!?)我想我在共享项目中也需要一个?

我把这个添加到我的Android项目中:

代码语言:javascript
运行
复制
using Android.Content;
using MyApp.Custom;
using MyApp.Droid.Custom;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace MyApp.Droid.Custom
{
    public class CustomWebViewRenderer : WebViewRenderer
    {
        public CustomWebViewRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
            Control.Settings.JavaScriptCanOpenWindowsAutomatically = true;
            Control.Settings.JavaScriptEnabled = true;
        }
    }
}

这是我的共同项目

代码语言:javascript
运行
复制
using Xamarin.Forms;

namespace MyKRing.Custom
{
    public class CustomWebView : WebView
    {
        
    }
}

然后在我的XAML中使用"CustomWebView“。

代码语言:javascript
运行
复制
<custom:CustomWebView x:Name="WebView"
                         HeightRequest="1000"
                         Source="{Binding WebViewContent}"
                         WidthRequest="1000" />

我的TestPageViewModel

代码语言:javascript
运行
复制
        public WebViewSource WebViewContent
        {
            get => _webViewContent; 
            set => SetProperty(ref _webViewContent, value);
        }

        public TestPageViewModel()
        {
            AmountEntryFrameVisible = true;
            NuapayFrameVisible = false;

            ConfirmLoadCommand = new Command(ExecuteConfirmLoadCommand);

            var localHtml = new HtmlWebViewSource();

            localHtml.Html = @"<html>
                                   <body>
                                    <p>This is a test</p>

<script src='https://testurl.com/test.js\'></script>
<script>
    TestJS.showUI('1234567890', 'https://testurl.com/ui/');
</script>
                                    </body>
                                </html>";

            WebViewContent = localHtml;

        }

目的是让JS在WebView加载页面时运行。

我离这儿很近吗?上面的问题是什么,因为它没有做任何事情(可以看到)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-10 06:31:48

ShowDialog 1.创建CustomWebView并创建方法.

代码语言:javascript
运行
复制
 public class CustomWebView : WebView
{
    Action<string> action;

    public static readonly BindableProperty UriProperty = BindableProperty.Create(
        propertyName: "Uri",
        returnType: typeof(string),
        declaringType: typeof(CustomWebView ),
        defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }

    public void RegisterAction(Action<string> callback)
    {
        action = callback;
    }

    public void Cleanup()
    {
        action = null;
    }


    public void ShowDialog(string data)
    {
        if (action == null || data == null)
        {
            return;
        }
        action.Invoke(data);
    }
}

2.使用CustomWebView

代码语言:javascript
运行
复制
   <local:CustomWebView x:Name="customWebView " Uri="index.html" />

ShowDiaplg 3.注册要从JavaScript调用的操作

代码语言:javascript
运行
复制
   customWebView.RegisterAction(data => DisplayAlert("Alert", DateTime.Now + "/n" + data, "OK"));

4.在Android平台上创建自定义呈现器.

CustomWebViewRenderer .cs:

代码语言:javascript
运行
复制
[assembly: ExportRenderer(typeof(CustomWebView ), typeof(CustomWebViewRenderer))]
namespace CustomRenderer.Droid
{
public class CustomWebViewRenderer: WebViewRenderer
{
    const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge2.showDialog(data);}";
    Context _context;

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

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

        if (e.OldElement != null)
        {
            Control.RemoveJavascriptInterface("jsBridge");
            ((CustomWebView )Element).Cleanup();
        }
        if (e.NewElement != null)
        {
            Control.SetWebViewClient(new JavascriptWebViewClient2(this, $"javascript: {JavascriptFunction}"));
            Control.AddJavascriptInterface(new JSBridge2(this), "jsBridge2");
            Control.LoadUrl($"file:///android_asset/Content/{((CustomWebView )Element).Uri}");
        }
    }

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

JavascriptWebViewClient2.cs:

代码语言:javascript
运行
复制
public class JavascriptWebViewClient2 : FormsWebViewClient
{
    string _javascript;

    public JavascriptWebViewClient2(CustomWebViewRenderer renderer, string javascript) : base(renderer)
    {
        _javascript = javascript;
    }

    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);
        view.EvaluateJavascript(_javascript, null);
    }
}

JSBridge2.cs:

代码语言:javascript
运行
复制
  public class JSBridge2 : Java.Lang.Object
{
    readonly WeakReference<CustomWebViewRenderer > hybridWebViewRenderer;

    public JSBridge2(CustomWebViewRenderer hybridRenderer)
    {
        hybridWebViewRenderer = new WeakReference<CustomWebViewRenderer >(hybridRenderer);
    }

   

    [JavascriptInterface]
    [Export("showDialog")]
    public void ShowDialog(string data)
    {
        CustomWebViewRenderer hybridRenderer;

        if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
        {
            ((CustomWebView)hybridRenderer.Element).ShowDialog(data);
        }
        //string ret = "Alert  :" + DateTime.Now;

        //return ret;
    }
}

我遵循下面链接中的代码示例。https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/customrenderers-hybridwebview/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67903119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档