Windows 上的 WebView 执行模式

此项平台特定功能可设置 WebView 托管其内容的线程。 通过将 WebView.ExecutionMode 可绑定属性设置为 WebViewExecutionMode 枚举值在 XAML 中使用它:

<ContentPage ...
             xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout>
        <WebView ... windows:WebView.ExecutionMode="SeparateThread" />
        ...
    </StackLayout>
</ContentPage>

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

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
...

WebView webView = new Xamarin.Forms.WebView();
webView.On<Windows>().SetExecutionMode(WebViewExecutionMode.SeparateThread);

WebView.On<Windows> 方法指定此项特定于平台的功能将仅在通用 Windows 平台上运行。 命名空间 Xamarin.Forms.PlatformConfiguration.WindowsSpecific 中的 WebView.SetExecutionMode 的方法用于设置 WebView 承载其内容的线程,其中 WebViewExecutionMode 枚举提供三个可能的值:

  • SameThread 指示内容托管在 UI 线程上。 这是 Windows 上 WebView 的默认值。
  • SeparateThread 指示内容托管在后台线程上。
  • SeparateProcess 指示内容托管在应用进程外的单独进程上。 每个 WebView 实例没有单独的进程,因此应用的所有 WebView 实例共享相同的单独进程。

此外,GetExecutionMode 该方法还可用于返回 WebView 的当前 WebViewExecutionMode