WebView.Eval() 在自定义呈现器 Web 视图中不起作用
WebView.Eval() is not working in a Custom Renderer Webview
我按照 xamarin pcl 示例 hybridwebview 从 JavaScript.
调用 C#
我还想实现一个从 C# 调用 JavaScript 的函数。
我尝试在 hybridwebview 示例之上使用 webview.Eval(script),但是它不会触发 webview 中的 JavaScript。我也尝试过没有自定义渲染的 webview,它工作正常。
在Webview CustomRenderer的情况下不能使用Eval..?
我的 CustomerRender:
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);
}
}
}
查看自定义渲染器和这一行
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
您使用的渲染器是ViewRenderer
不是WebViewRenderer
,这里的HybridWebView不是webview,它只是一个普通视图,所以Eval
功能无效。
我想到的解决方法:使用消息中心并在自定义渲染器中执行 js
Forms 项目
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);
}
我的测试
我按照 xamarin pcl 示例 hybridwebview 从 JavaScript.
调用 C#我还想实现一个从 C# 调用 JavaScript 的函数。
我尝试在 hybridwebview 示例之上使用 webview.Eval(script),但是它不会触发 webview 中的 JavaScript。我也尝试过没有自定义渲染的 webview,它工作正常。
在Webview CustomRenderer的情况下不能使用Eval..?
我的 CustomerRender:
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);
}
}
}
查看自定义渲染器和这一行
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
您使用的渲染器是ViewRenderer
不是WebViewRenderer
,这里的HybridWebView不是webview,它只是一个普通视图,所以Eval
功能无效。
我想到的解决方法:使用消息中心并在自定义渲染器中执行 js
Forms 项目
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);
}