Reputation: 36341
I used NuGet Package Manager to install https://github.com/cefsharp/CefSharp
Which added the item to my .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>
Next I modified my xaml file to look like this:
<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>
Lastly, I added the following constructor in the App.xaml.cs
file:
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);
}
}
}
When I run the application, I am getting the following error:
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()
Why could it not load the package?
Edit:
Here is the new file structure after updating:
Upvotes: 0
Views: 9705
Reputation: 4420
Can you upgrade to netcoreapp3.1? If yes then switching to https://www.nuget.org/packages/CefSharp.Wpf.NETCore/ should resolve the problem.
The Badimageformatexception is rather misleading in the context of loading a C++/CLI assembly, any failure will give this exception. See https://github.com/dotnet/runtime/issues/31743#issuecomment-582168696 for background.
If you need to stay with netcoreapp3.0 then you'll need to either specify a PlatformTarget or a RuntimeIdentifier in your project file.
Upvotes: 0
Reputation: 11
Is it using the correct dll? My initial guess is that there is a mismatch between the Program architecture and the used CefSharp.Core.dll, e.g. the program runs in 64 bit mode but references the 32bit assembly.
Upvotes: 0