我在本地發布了一個 C# WPF MVVM(Caliburn Micro) 應用程式,當我嘗試使用 WebView2 顯示網頁時出現以下事件查看器訊息。該網頁在除錯中顯示良好。
說明:行程因未處理的例外而終止。例外資訊:System.DllNotFoundException:找不到 Dll。在 Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(字串 browserExecutableFolder,字串 userDataFolder,ICoreWebView2EnvironmentOptions 選項,ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler environment_created_handler)在 Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(字串 browserExecutableFolder,字串 userDataFolder) Views.BrowserView.InitializeAsync()
我看到它與 userDataFolder 相關,并嘗試了一些解決堆疊溢位的方法,但仍然沒有運氣。
我不明白的是似乎創建了一個 EBWebView 檔案夾,我認為它是 userData 檔案夾,所以為什么會出錯,是否存在一些權限問題?
WebView2Loader.dll 在發布目錄中
EBWebView 的位置是:C:\Users*****\AppData\Local\Temp\IGPC2WPF\EBWebView
這是顯示網站的檔案的 xaml 和代碼。
XAML 視圖
<UserControl x:Class="IGPC2WPF.Views.BrowserView"
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:local="clr-namespace:IGPC2WPF.Views"
xmlns:cal="http://caliburnmicro.com"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d" Background="#3b3b3b"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<wv2:WebView2 x:Name="WebView" Source="{Binding PdfAddress, Mode=TwoWay}" />
</Grid>
視圖模型
private string _address;
private readonly IEventAggregator _events;
private readonly IConfiguration _config;
public string Address
{
get { return _address; }
set { _address = value; NotifyOfPropertyChange(() => Address); }
}
public BrowserViewModel(IEventAggregator events, IConfiguration config)
{
_events = events;
_config = config;
_address = _config.GetValue<string>("MiscURLs:WorkToListReport");
}
public void Close()
{
_events.PublishOnUIThreadAsync(new GenerateExcelDataEvent());
TryCloseAsync();
}
代碼背后:
public partial class BrowserView : UserControl
{
public WebPageView()
{
InitializeComponent();
InitializeAsync();
}
async void InitializeAsync()
{
string userDataFolder = Path.Combine(Path.GetTempPath(), "IGPC2WPF");
WebView.CreationProperties = new()
{
UserDataFolder = userDataFolder
};
await WebView.EnsureCoreWebView2Async(null);
}
}
編輯 好的,問題是當我在發布目錄中運行 setup.exe 時,存在的 WebView2Loader.dll 沒有被復制到安裝目錄。如果我手動將其移動到安裝位置,則網頁顯示正常。
如何確保在 ClickOnce 中復制此 dll?
uj5u.com熱心網友回復:
我發現了一個類似的問題,它似乎已經解決了 WebView2Loader.dll 未安裝到根最終安裝檔案夾的問題。
在 .csproj 檔案中,我添加了:
<PropertyGroup>
<WebView2LoaderPreference>Static</WebView2LoaderPreference>
</PropertyGroup>
<ItemGroup>
<PublishFile Include="runtimes\win-x64\native\WebView2Loader.dll">
<Visible>False</Visible>
<Group>
</Group>
<TargetPath>WebView2Loader.dll</TargetPath>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>File</FileType>
</PublishFile>
</ItemGroup>
現在,這會將所需的檔案復制到根目錄中,并且應用程式 webview 頁面可以正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436199.html
