You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.5 KiB
45 lines
1.5 KiB
using Android.Content;
|
|
using Demo.Droid.Infrastructure;
|
|
using Demo.Infrastructure;
|
|
using Xamarin.Forms;
|
|
using Xamarin.Forms.Platform.Android;
|
|
|
|
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
|
|
|
|
namespace Demo.Droid.Infrastructure
|
|
{
|
|
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
|
|
{
|
|
private const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";
|
|
private Context _context;
|
|
|
|
public HybridWebViewRenderer(Context context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
|
|
{
|
|
base.OnElementChanged(e);
|
|
|
|
if (Control == null)
|
|
{
|
|
var webView = new Android.Webkit.WebView(_context);
|
|
webView.Settings.JavaScriptEnabled = true;
|
|
webView.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
|
|
SetNativeControl(webView);
|
|
}
|
|
if (e.OldElement != null)
|
|
{
|
|
Control.RemoveJavascriptInterface("jsBridge");
|
|
var hybridWebView = e.OldElement as HybridWebView;
|
|
hybridWebView.Cleanup();
|
|
}
|
|
if (e.NewElement != null)
|
|
{
|
|
Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
|
|
Control.LoadUrl(Element.Uri);
|
|
}
|
|
}
|
|
}
|
|
} |