c# - 无法加载文件或程序集 CefSharp.Core.Runtime
问题描述
我使用 NuGet 包管理器安装https://github.com/cefsharp/CefSharp
哪个将该项目添加到我的.csproj
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CefSharp.Wpf" Version="87.1.132"/>
</ItemGroup>
</Project>
接下来我修改了我的 xaml 文件,如下所示:
<Window x:Class="csharp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:csharp"
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<wpf:ChromiumWebBrowser x:Name="webBrowser" Address="http://localhost:4200">
</wpf:ChromiumWebBrowser>
</Grid>
</Window>
最后,我在文件中添加了以下构造函数App.xaml.cs
:
namespace csharp {
public partial class App : Application {
public App() {
var settings = new CefSettings() { // Line 12
CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
};
settings.CefCommandLineArgs.Add("enable-media-stream");
settings.CefCommandLineArgs.Add("use-fake-ui-for-media-stream");
settings.CefCommandLineArgs.Add("enable-usermedia-screen-capturing");
var dependencyCheck = true;
Cef.Initialize(settings, performDependencyCheck: dependencyCheck, browserProcessHandler: null);
}
}
}
当我运行应用程序时,我收到以下错误:
Exception has occurred: CLR/System.BadImageFormatException
An unhandled exception of type 'System.BadImageFormatException' occurred in CefSharp.Wpf.dll: 'Could not load file or assembly 'CefSharp.Core.Runtime, Version=87.1.132.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138'. An attempt was made to load a program with an incorrect format.'
at CefSharp.CefSettingsBase..ctor()
at CefSharp.Wpf.CefSettings..ctor()
at csharp.App..ctor() in App.xaml.cs:line 12
at csharp.App.Main()
为什么它不能加载包?
编辑:
这是更新后的新文件结构:
解决方案
你能升级到netcoreapp3.1吗?如果是,那么切换到https://www.nuget.org/packages/CefSharp.Wpf.NETCore/应该可以解决问题。
Badimageformatexception 在加载 C++/CLI 程序集的上下文中相当具有误导性,任何失败都会给出此异常。有关背景,请参阅https://github.com/dotnet/runtime/issues/31743#issuecomment-582168696。
如果您需要继续使用 netcoreapp3.0,则需要在项目文件中指定 PlatformTarget 或 RuntimeIdentifier。
推荐阅读
- javascript - 如何修复代码,以便仅针对点击的国家/地区显示附加卡?
- javascript - 如何使用 javascript/jquery 验证 pkcs#7 格式的签名消息
- python - 如何循环遍历熊猫数据框中列中的每一行
- python - BeautifulSoup 和 Selenium 抓取网页,汤不反映更新的 HTML
- javascript - 阻止模态内容默认打开
- mysql - Redis 连接到 my-redis:6379 失败 - 运行种子时 getaddrinfo ENOTFOUND
- node.js - 由于与 babel 的依赖关系,安装 react 时出现问题
- bash - wp-cli 表输出到变量-> wlack webhook
- bluetooth - 每个信标可以跟踪多少个BLE设备以进行室内定位?
- c# - Sublime Text 3 自动缩进 my } else { 行