We're trying to create a custom renderer for a WebView. The only issue is that Xamarin can't find Android.Webkit.WebView.
using [AppName];
using [AppName].Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Mono;
using Android.Webkit;
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace [AppName].Droid
{
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
{
const string JavaScriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var webView = new Android.Webkit.WebView(Forms.Context);
webView.Settings.JavaScriptEnabled = true;
SetNativeControl(webView);
}
if (e.OldElement != null)
{
Control.RemoveJavascriptInterface("jsBridge");
var hybridWebView = e.OldElement as HybridWebView;
}
if (e.NewElement != null)
{
Control.LoadUrl(string.Format("file:///android_asset/Content/{0}", Element.Source));
InjectJS(JavaScriptFunction);
}
}
void InjectJS(string script)
{
if (Control != null)
{
Control.LoadUrl(string.Format("javascript: {0}", script));
}
}
}
We have 'using Android.Webkit' but are met with the error 'the type or namespace name 'Webkit' does not exist in the namespace `[AppName].Android'. Are you missing an assembly reference?'
It seems like it's looking for it inside of our app namespace, which seems it could be the issue. But we have no idea how to resolve it.
Any help would be greatly appreciated.
It sounds like it is getting confused with another Android namespace in your project. Try using the global namespace
global::Android.Webkit.WebView
you could also define an alias
using web = global::Android.Webkit.WebView;