Android 上的 WebView 混合内容

此 .NET Multi-platform App UI (.NET MAUI) Android 平台特定功能用于控制 WebView 是否可以显示混合内容。 混合内容是最初通过 HTTPS 连接加载的内容,但它通过 HTTP 连接加载资源(如图像、音频、视频、样式表、脚本)。 其使用方式为,在 XAML 中将 WebView.MixedContentMode 附加属性设置为 MixedContentHandling 枚举的值:

<ContentPage ...
             xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls">
    <WebView ... android:WebView.MixedContentMode="AlwaysAllow" />
</ContentPage>

或者,可以使用 Fluent API 从 C# 使用它:

using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
...

webView.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().SetMixedContentMode(MixedContentHandling.AlwaysAllow);

WebView.On<Microsoft.Maui.Controls.PlatformConfiguration.Android> 方法指定此平台特定仅在 Android 上运行。 WebView.SetMixedContentMode 命名空间中的 Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific 方法用于控制是否可以显示混合内容,其中 MixedContentHandling 枚举提供三个可能的值:

  • AlwaysAllow – 指示 WebView 允许 HTTPS 源从 HTTP 源加载内容。
  • NeverAllow – 指示 WebView 不允许 HTTPS 源从 HTTP 源加载内容。
  • CompatibilityMode – 指示 WebView 将尝试与最新设备 Web 浏览器的方法兼容。 某些 HTTP 内容可能允许由 HTTPS 源加载,而其他类型的内容将被阻止。 阻止或允许的内容类型可能会随每个操作系统版本而变化。

结果是,指定的 MixedContentHandling 值被应用于 WebView,后者控制是否可以显示混合内容:

WebView mixed content handling platform-specific.