首页 > 解决方案 > 在 Powershell 中运行 CefSharp

问题描述

我需要显示从 Powershell 脚本运行的 CefSharp(WPF 或 Winforms)Webbrowser 控件。我从 nuget 包中提取了 x84 dll,并尝试像这样添加它们:

try{
    Add-Type -Path $PSScriptRoot/CefSharp.Core.dll
    Add-Type -Path $PSScriptRoot/CefSharp.BrowserSubprocess.Core.dll
    Add-Type -Path $PSScriptRoot/CefSharp.dll
    Add-Type -Path $PSScriptRoot/CefSharp.WinForms.dll
}
catch{

$_.Exception.Message
}

我收到错误消息:

无法加载文件...或依赖。未找到模块。

有没有办法通过 Powershell 使用 CefSharp?

标签: powershellcefsharp

解决方案


我得到了和你一样的错误。

我通过在与 CefSharp dll 相同的文件夹中添加 CEF redist 包来解决这个问题:

https://www.nuget.org/packages/cef.redist.x64/

https://www.nuget.org/packages/cef.redist.x86/

CEF官方资料: https ://bitbucket.org/chromiumembedded/cef/src/master/

我为其他人发布了我的示例代码:

[System.Reflection.Assembly]::LoadFile("$AssPath\CefSharp.Core.dll")
[System.Reflection.Assembly]::LoadFile("$AssPath\CefSharp.WinForms.dll")
[System.Reflection.Assembly]::LoadFile("$AssPath\CefSharp.dll")

Add-Type -AssemblyName System.Windows.Forms

# WinForm Setup
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,9"
$mainForm.ForeColor = [System.Drawing.Color]::White
$mainForm.BackColor = [System.Drawing.Color]::DarkSlateBlue
$mainForm.Text = "CefSharp"
$mainForm.Width = 960
$mainForm.Height = 700

[CefSharp.WinForms.ChromiumWebBrowser] $browser = New-Object CefSharp.WinForms.ChromiumWebBrowser "www.google.com"
$mainForm.Controls.Add($browser)

[void] $mainForm.ShowDialog()

推荐阅读