【问题标题】:WebView.Eval() is not working in a Custom Renderer WebviewWebView.Eval() 在自定义渲染器 Web 视图中不起作用
【发布时间】:2019-11-15 16:58:59
【问题描述】:

我已按照 xamarin pcl 示例 hybridwebview 从 JavaScript 调用 C#。

我还想实现一个从 C# 调用 JavaScript 的函数。

我尝试在 hybridwebview 示例之上使用 webview.Eval(script),但是它不会触发 webview 中的 JavaScript。我还尝试了一个没有自定义渲染的 webview,它工作正常。

在Webview CustomRenderer的情况下不能使用Eval..?

我的客户呈现:

public class HybridWebView : WebView
{
  Action<string> action;
  public static readonly BindableProperty UriProperty = BindableProperty.Create (
    propertyName: "Uri",
    returnType: typeof(string),
    declaringType: typeof(HybridWebView),
    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 InvokeAction (string data)
  {
    if (action == null || data == null) {
      return;
    }
    action.Invoke (data);
  }
}

在 ContentPage 中调用 Eval:

namespace CustomRenderer
{
    public partial class HybridWebViewPage : ContentPage
    {
        public Func<string, Task<string>> EvaluateJavascript { get; set; }

        public HybridWebViewPage ()
        {
            InitializeComponent ();

            hybridWebView.RegisterAction (data => DisplayAlert ("Alert", "Hello " + data, "OK"));
        }

        private void Button_Clicked(object sender, System.EventArgs e)
        {
            var _Script = string.Format("alert('test')");
            hybridWebView.Eval(_Script);
        }
    }
}

【问题讨论】:

    标签: c# xamarin.forms webview


    【解决方案1】:

    查看自定义渲染器和这一行

    public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
    

    你使用的渲染器是ViewRenderer而不是WebViewRenderer,这里的HybridWebView不是webview,只是普通视图,所以Eval功能无效。

    我想到的解决方法:使用消息中心并在自定义渲染器中执行js

    表单项目

    private void Button_Clicked(object sender, System.EventArgs e)
        {
            var _Script = string.Format("alert('test')");
            MessagingCenter.Send<object,string>(this,"Hi", _Script);
        }
    

    自定义渲染器

          if (Control == null)
            {
                var webView = new Android.Webkit.WebView(_context);
                webView.Settings.JavaScriptEnabled = true;
    
                webView.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
    
                ////////////////////////add here 
                webView.SetWebChromeClient(new WebChromeClient());
                MessagingCenter.Subscribe<object, string>(this, "Hi", (obj, arg) =>
                {
                    webView.EvaluateJavascript(arg, null);
                });
                ///////////////////////
    
                SetNativeControl(webView);
    
            }
    

    我的测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 2018-01-06
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多