我使用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()
为什么它不能装载包裹呢?
编辑:
以下是更新后的新文件结构:
发布于 2021-02-16 22:09:27
它是否使用了正确的dll?我最初的猜测是,程序架构和使用的CefSharp.Core.dll之间存在不匹配,例如程序以64位模式运行,但引用32位程序集。
发布于 2021-02-16 22:28:10
你能升级到netcoreapp3.1吗?如果是,那么切换到https://www.nuget.org/packages/CefSharp.Wpf.NETCore/应该可以解决这个问题。
在加载C++/CLI程序集的上下文中,Badimageformatexception是相当误导的,任何失败都会导致此异常。有关背景信息,请参见https://github.com/dotnet/runtime/issues/31743#issuecomment-582168696。
如果您需要继续使用netcoreapp3.0,那么您需要在项目文件中指定一个PlatformTarget或RuntimeIdentifier。
https://stackoverflow.com/questions/66232559
复制相似问题