簡體   English   中英

如何使用 wpf 在 blazor 上設置 WebView2 設置

[英]How to set WebView2 settings on blazor with wpf

我正在創建一個用 blazor 和 wpf 制作的應用程序,我想知道如何設置 WebView2 設置。

我使用Microsoft.AspNetCore.Components.WebView.Wpf

我的XAML文件具有以下代碼:

<wv:BlazorWebView HostPage="wwwroot/index.html" Services="{StaticResource services}">
     <wv:BlazorWebView.RootComponents>
          <wv:RootComponent Selector="#app" ComponentType="{x:Type local:main}" />
     </wv:BlazorWebView.RootComponents>
</wv:BlazorWebView>

C#代碼中,我有以下內容:

using Microsoft.AspNetCore.Components.WebView.WebView2;

public partial class MainWindow : Window
{
    private readonly ICoreWebView2Wrapper _WebView;

    public MainWindow()
    {
        var blazorCollection = new ServiceCollection();
        blazorCollection.AddBlazorWebView();
        Resources.Add("services", blazorCollection.BuildServiceProvider());
        InitializeComponent();
        Initialize();
    }

    private void Initialize()
    {
        _WebView.Settings.IsZoomControlEnabled = false;
        _WebView.Settings.AreDevToolsEnabled = false;
    }
}

我得到CS0649在編譯的private readonly ICoreWebView2Wrapper _WebView;

當我嘗試編譯並嘗試打開應用程序時,出現異常

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

NullReferenceException: Object reference not set to an instance of an object.

已經添加了這些 WebView2 設置的人可以告訴我如何正確使用它們。

在您的 xaml 中添加對您的 WebView2 的引用:

<blazor:BlazorWebView HostPage="wwwroot\\index.html" Services="{StaticResource Services}" x:Name="blazorWebView1">

而不是調用你的 Initialize() 做:
Loaded += MainWindow_Loaded;

private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
      await blazorWebView1.WebView.EnsureCoreWebView2Async();
      blazorWebView1.WebView.CoreWebView2.Settings.IsZoomControlEnabled = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM