无法加载文件或程序集CefSharp.Core.Runtime

问题描述 投票:0回答:2

我使用NuGet Package Manager安装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# wpf cefsharp
2个回答
0
投票

它使用了正确的dll吗?我最初的猜测是程序架构和使用的 CefSharp.Core.dll 之间不匹配,例如该程序以 64 位模式运行,但引用 32 位程序集。


0
投票

可以升级到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。

© www.soinside.com 2019 - 2024. All rights reserved.