首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法加载文件或程序集CefSharp.Core.Runtime

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

Stack Overflow用户
提问于 2021-02-16 21:39:04
回答 2查看 6.6K关注 0票数 0

我使用NuGet软件包管理器安装https://github.com/cefsharp/CefSharp

它将该项添加到我的.csproj

代码语言:javascript
运行
复制
<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文件,如下所示:

代码语言:javascript
运行
复制
<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文件中添加了以下构造函数:

代码语言:javascript
运行
复制
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);
    }
  }
}

当我运行应用程序时,我会得到以下错误:

代码语言:javascript
运行
复制
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()

为什么它不能装载包裹呢?

编辑:

以下是更新后的新文件结构:

EN

回答 2

Stack Overflow用户

发布于 2021-02-16 22:09:27

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

票数 0
EN

Stack Overflow用户

发布于 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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66232559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档