简体   繁体   English

在 WPF/MVVM/Caliburn 设置中设置 WebView2 数据文件夹

[英]Set WebView2 data folder in WPF/MVVM/Caliburn setup

We have WPF app which is using Caliburn/MVVM setup.我们有使用 Caliburn/MVVM 设置的 WPF 应用程序。
ViewModel registration in Bootstrapper : Bootstrapper中的ViewModel注册:

builder.RegisterType<CustomWebViewModel>().As<Screen>();  

WebView2 creation in CustomWebView.xaml :CustomWebView.xaml中创建WebView2

<Grid>
    <wv2:WebView2 x:Name="WebView2" Source="{Binding WebAddress, Mode=OneWay, 
        UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" />
</Grid>  

In developement environment app runs smoothly: WebView2 shows correct content.在开发环境应用程序运行顺利: WebView2显示正确的内容。
However in production this app is installed in C:\Program Files\Company\Product\ folder.但是在生产中,此应用程序安装在C:\Program Files\Company\Product\文件夹中。
When started it throws message:启动时会抛出消息:

Microsoft Edge can't read and write to its data directory: C:\Program Files\Company\Product\Product.exe.WebView2\EBWebView Microsoft Edge 无法读取和写入其数据目录:C:\Program Files\Company\Product\Product.exe.WebView2\EBWebView

The reason is that WebView2 needs to be configured to use data folder that app would have a permission to use.原因是 WebView2 需要配置为使用应用程序有权使用的数据文件夹。
This piece of code does it:这段代码做到了:

        var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        var userDataFolder = System.IO.Path.Combine(appData, "CustomWebView2");

        var task = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
        var webView2 = (WebView2)this.FindName("WebView2");
        if (webView2 != null)
            await webView2.EnsureCoreWebView2Async(task);

Question: where do I call this piece of code in this setup?问题:在这个设置中我在哪里调用这段代码?
I tried OnCoreWebView2InitializationCompleted and OnContentLoading but in both cases EnsureCoreWebView2Async throws:我尝试OnCoreWebView2InitializationCompletedOnContentLoading但在这两种情况下EnsureCoreWebView2Async抛出:

System.ArgumentException: ' WebView2 was already initialized with a different CoreWebView2Environment . System.ArgumentException: ' WebView2 已经用不同的 CoreWebView2Environment 进行了初始化 Check to see if the Source property was already set or EnsureCoreWebView2Async was previously called with different values.'检查 Source 属性是否已设置,或者 EnsureCoreWebView2Async 之前是否使用不同的值调用过。

Call EnsureCoreWebView2Async before you set the Source of the WebView2 control.在设置WebView2控件的Source之前调用EnsureCoreWebView2Async

So remove the binding from your XAML markup:因此,从 XAML 标记中删除绑定:

<wv2:WebView2 x:Name="WebView2" />

...and set it programmatically: ...并以编程方式设置它:

var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var userDataFolder = System.IO.Path.Combine(appData, "CustomWebView2");

var task = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
var webView2 = (WebView2)this.FindName("WebView2");
if (webView2 != null)
{
    await webView2.EnsureCoreWebView2Async(task);
    webView2.SetBinding(WebView2.SourceProperty, new Binding("WebAddress"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM