Xamarin.Forms ERR_FILE_NOT_FOUND (file:///android_asset/Content/https://stackoverflow.com) 使用 HybridWebViewSample
Xamarin.Forms ERR_FILE_NOT_FOUND (file:///android_asset/Content/https://stackoverflow.com) using HybridWebViewSample
我按照 post 创建了一个自定义 WebView 来与客户端交互 JavaScript:https://docs.microsoft.com/es-es/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
但是当我尝试加载页面时,例如 https://www.google.es, the wevView shows an "ERROR_FILE_NOT_FOUND" loading the page "file:///android_asset/Content/https://www.google.es"
这是 HybridWebViewRenderer 的 OnElementChanged:
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
Control.RemoveJavascriptInterface("jsBridge");
((HybridWebView)Element).Cleanup();
}
if (e.NewElement != null)
{
Control.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
Control.LoadUrl($"file:///android_asset/Content/{((HybridWebView)Element).Uri}");
}
}
已编辑:hybridWebView 似乎不适用于外部页面!
有什么建议吗?
已解决:
更改 LoadUrl 方法:
Control.LoadUrl($"file:///android_asset/Content/{((HybridWebView)Element).Uri}");
作者:
UrlWebViewSource source = Element.Source as UrlWebViewSource;
Control.LoadUrl(source.Url);
适用于外部网站,可以检索 JavaScript 回调并发送 JavaScript 代码。这是完整代码:
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
Control.RemoveJavascriptInterface("jsBridge");
((HybridWebView)Element).Cleanup();
}
if (e.NewElement != null)
{
Control.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
UrlWebViewSource source = Element.Source as UrlWebViewSource;
Control.LoadUrl(source.Url);
}
}
我按照 post 创建了一个自定义 WebView 来与客户端交互 JavaScript:https://docs.microsoft.com/es-es/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
但是当我尝试加载页面时,例如 https://www.google.es, the wevView shows an "ERROR_FILE_NOT_FOUND" loading the page "file:///android_asset/Content/https://www.google.es"
这是 HybridWebViewRenderer 的 OnElementChanged:
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
Control.RemoveJavascriptInterface("jsBridge");
((HybridWebView)Element).Cleanup();
}
if (e.NewElement != null)
{
Control.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
Control.LoadUrl($"file:///android_asset/Content/{((HybridWebView)Element).Uri}");
}
}
已编辑:hybridWebView 似乎不适用于外部页面!
有什么建议吗?
已解决:
更改 LoadUrl 方法:
Control.LoadUrl($"file:///android_asset/Content/{((HybridWebView)Element).Uri}");
作者:
UrlWebViewSource source = Element.Source as UrlWebViewSource;
Control.LoadUrl(source.Url);
适用于外部网站,可以检索 JavaScript 回调并发送 JavaScript 代码。这是完整代码:
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
Control.RemoveJavascriptInterface("jsBridge");
((HybridWebView)Element).Cleanup();
}
if (e.NewElement != null)
{
Control.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
UrlWebViewSource source = Element.Source as UrlWebViewSource;
Control.LoadUrl(source.Url);
}
}