Android 上的 WebView 缩放

此 .NET Multi-platform App UI (.NET MAUI) Android 平台特定功能可在 WebView 上启用捏合缩放和缩放控件。 其使用方式为,在 XAML 中将 WebView.EnableZoomControlsWebView.DisplayZoomControls 可绑定属性设置为 boolean 值:

<ContentPage ...
             xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls">
    <WebView Source="https://www.microsoft.com"
             android:WebView.EnableZoomControls="true"
             android:WebView.DisplayZoomControls="true" />
</ContentPage>

WebView.EnableZoomControls 可绑定属性控制是否在 WebView 上启用捏合缩放,而 WebView.DisplayZoomControls 可绑定属性控制是否在 WebView 上覆盖缩放控件。

或者,可以通过 Fluent API 从 C# 使用平台特定功能:

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

webView.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>()
    .EnableZoomControls(true)
    .DisplayZoomControls(true);

WebView.On<Microsoft.Maui.Controls.PlatformConfiguration.Android> 方法指定这一平台特定功能仅可在 Android 上运行。 Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific 命名空间中的 WebView.EnableZoomControls 方法用于控制是否在 WebView 上启用捏合缩放。 同一命名空间中的 WebView.DisplayZoomControls 方法用于控制是否在 WebView 上覆盖缩放控件。 此外,WebView.ZoomControlsEnabledWebView.ZoomControlsDisplayed 方法还可用于返回是否分别启用捏合缩放和缩放控件的结果。

结果是可以在 WebView 上启用捏合缩放,并可以在 WebView 上覆盖缩放控件:

Screenshot of zoomed WebView on Android.

重要

必须同时通过相应的可绑定属性或方法启用和显示缩放控件,才能覆盖在 WebView 上。