wpf - Webview2 在 WPF .Net 核心应用程序设置中不起作用

标签 wpf .net-core mvvm setup-project webview2

我们已经为 WebView2 安装了 Microsoft Edge WebView2 runtime(x86)。引用下面的 SS。

enter image description here

我们正在使用 .NET Core 3.1 框架和 WPF 进行应用程序开发。 我们的 webview2 在 Visual Studio 中的调试/发布/ Release模式下运行良好,但一旦我们安装了我们的应用程序,它就不会被加载。

当我们以正常权限运行应用程序并且也没有加载时,它也没有给出任何错误。 当我们以“以管理员身份运行”运行它时,它会出现以下错误,

enter image description here

我们在 Visual Studio 中使用 Windows Installer 项目扩展来创建 .MSI

我们的 XAML 代码如下,

<UserControl x:Class="SampleApp.Views.SampleWebView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="0.7*"/>
            <ColumnDefinition Width="0.3*"/>
            <ColumnDefinition Width="10"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="1"  Margin="5" CornerRadius="10"  BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Border.BitmapEffect>
                <DropShadowBitmapEffect Color="Black" Direction="40" ShadowDepth="2" Softness="1" Opacity="0.3" />
            </Border.BitmapEffect>
            <wv2:WebView2 x:Name="MyWebView" Source="{Binding UrlToGo}" Margin="5 2"/>
        </Border>
</Grid>

UrlToGo 来 self 们的 ViewModel。

请帮助我们解决。

最佳答案

错误说明了一切,它没有对要创建 UserDataFolder(包含缓存和 cookie 等)的“C:\Program files”的写入权限。

解决方法是在创建WebView2时指定UserDataFolder。你这样做:

using Microsoft.Web.WebView2.Core;

string userDataFolder = Path.Combine(Path.GetTempPath(), "WhatEver");
webView21.CreationProperties = new CoreWebView2CreationProperties()
{
    UserDataFolder = userDataFolder
};
await webView21.EnsureCoreWebView2Async();

重要的是,在调用 EnsureCoreWebView2Async 之前,不要设置 Source 属性 当然,将“webView2”替换为您的 WebView2 控件的名称。

关于wpf - Webview2 在 WPF .Net 核心应用程序设置中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68111881/

相关文章:

c# - ASP.NET Core Web API 中的过滤结果

WPF MVVM 管理多个窗口

c# - 何时在 windows phone 项目中实现 INotifyPropertyChanged

wpf - ResourceDictionary 中 DataTemplate 的代码隐藏

wpf - 如何使UserControl在运行时自行删除(在WPF中)?

c# - 带有 ASP-NET 和 Angular 的 Visual Studio 在浏览器中显示 TimeoutException

c# - 将 DataGrid 绑定(bind)到 ListBox 选定项

WPF:在绑定(bind)到可观察集合的选项卡控件中隐藏选项卡项

wpf - MVVM:模型状态更改时通知 View

c# - 迁移 .NET 核心 2.0 缺少 IActionSelectorDecisionTreeProvider