我尝试通过可能没有 SSL 证书的 Web 查看器访问网站。 不幸的是我总是遇到黑屏。
private async void lwConnections_ItemTapped(object sender, Syncfusion.Maui.ListView.ItemTappedEventArgs e)
{
if (lwConnections.SelectedItem != null)
{
await Shell.Current.GoToAsync($"{nameof(WebViewer)}?URL={((Connection)lwConnections.SelectedItem).URL}");
}
}
有人有解决办法吗?
我对 .NET MAUI 非常陌生,找不到可以实施的解决方案。
您在这里看到的是 WebView 控件的背景颜色。
Android 已捕获 SSL 错误,但没有向您显示任何内容。
将此类放入 Android 命名空间中
namespace AppName.Platforms.Android
{
using global::Android.Net.Http;
using global::Android.Webkit;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
public class CustomWebViewClient : MauiWebViewClient
{
public CustomWebViewClient(WebViewHandler handler) : base(handler)
{
}
public override void OnReceivedSslError(global::Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
{
//this line make ssl error handle so the webview show the page even with certificate errors
handler.Proceed();
}
}
}
然后制作自定义控件
namespace AppName.Controls
{
internal class CustomWebView : WebView
{
}
}
将新版本添加到您的 xaml 中
<controls:CustomWebView Grid.Row="1" Source="{Binding TargetUrl}" Background="BlueViolet" />
最后将映射添加到您的应用程序启动中
WebViewHandler.Mapper.AppendToMapping("CustomWebView", (handler, view) =>
{
if (view is CustomWebView)
{
#if ANDROID
if (handler is WebViewHandler platformHandler)
{
WebViewHandler.Mapper.AppendToMapping(nameof(Android.Webkit.WebViewClient), (handler, view) => { handler.PlatformView.SetWebViewClient(new CustomWebViewClient(platformHandler)); });
}
#endif
}
});