首页 > 解决方案 > 无法加载文件或程序集 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()

文件

为什么它不能加载包?

编辑:

这是更新后的新文件结构:

新的文件结构

标签: c#wpfcefsharp

解决方案


你能升级到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。


推荐阅读