[英]How to set WebView2 settings on blazor with wpf
I'm creating an app made in blazor and wpf, I would like to know how I can set the WebView2 settings.我正在创建一个用 blazor 和 wpf 制作的应用程序,我想知道如何设置 WebView2 设置。
I use Microsoft.AspNetCore.Components.WebView.Wpf packages我使用Microsoft.AspNetCore.Components.WebView.Wpf包
My XAML file has the following code:我的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>
In C# code I have the following:在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;
}
}
I get CS0649 on compilation for private readonly ICoreWebView2Wrapper _WebView;
我得到CS0649在编译的private readonly ICoreWebView2Wrapper _WebView;
When I tried to compile and I try to open the application I get exception当我尝试编译并尝试打开应用程序时,出现异常
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
NullReferenceException: Object reference not set to an instance of an object.
Someone who has already added these WebView2 settings can tell me how to use them correctly.已经添加了这些 WebView2 设置的人可以告诉我如何正确使用它们。
Add a reference to your WebView2 in your xaml:在您的 xaml 中添加对您的 WebView2 的引用:
<blazor:BlazorWebView HostPage="wwwroot\\index.html" Services="{StaticResource Services}" x:Name="blazorWebView1">
In stead of calling your Initialize() do:而不是调用你的 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.