如何正确使用 CefSharp for WPF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24999355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:14:27  来源:igfitidea点击:

How to use CefSharp for WPF properly?

wpfxamlc#-4.0visual-studio-2012cefsharp

提问by Flo

I created a new Wpf Project with VS2012. I right clicked on the Project and chose "Manage NuGet Packages". Then i installed the CefSharp Package for Wpf.

我用 VS2012 创建了一个新的 Wpf 项目。我右键单击该项目并选择“管理 NuGet 包”。然后我为 Wpf 安装了 CefSharp 包。

Then i used this "guide" : https://github.com/cefsharp/CefSharp/blob/master/README.WPF.md

然后我使用了这个“指南”:https: //github.com/cefsharp/CefSharp/blob/master/README.WPF.md

Sadly i get like 4 Errors and i don't know how to get rid of them!

可悲的是,我得到了 4 个错误,我不知道如何摆脱它们!

These are the errors i get (i took out the path to the project with "filepath"):

这些是我得到的错误(我用“文件路径”取出了项目的路径):

Error   5   The type 'cefSharp:WebView' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.    "filepath"\Chromium\MainWindow.xaml 6   10  Chromium
Error   3   The name "WebView" does not exist in the namespace "clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf".  "filepath"\Chromium\MainWindow.xaml 6   9   Chromium

Error   6   The name 'Cef' does not exist in the current context    "filepath"\Chromium\MainWindow.xaml.cs  28  13  Chromium
Error   4   Assembly 'CefSharp.Wpf' was not found. Verify that you are not missing an assembly reference. Also, verify that your project and all referenced assemblies have been built. "filepath"\Chromium\MainWindow.xaml 4   22  Chromium

My XAML for the MainWindow:

我的主窗口 XAML:

<Window x:Class="Chromium.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" Title="MainWindow" Height="350" Width="525">
<Grid>
    <cefSharp:WebView x:Name="WebView" />
</Grid>

Code behind for MainWindow.cs:

MainWindow.cs 背后的代码:

using System.ComponentModel;
using System.Windows;
using CefSharp;

namespace Chromium
{
    public partial class MainWindow 
    {
        public MainWindow()
        {
            InitializeComponent();

            WebView.PropertyChanged += OnWebViewPropertyChanged;

            Cef.Initialize(new Settings());
        }

        private void OnWebViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "IsBrowserInitialized":
                    if (WebView.IsBrowserInitialized)
                    {
                        WebView.Load("http://10.211.55.2:42000");
                    }

                    break;
            }
        }
    }
}

The XAML and Code behind for the MainWindow are pretty much exactly the same as in the README.MD

MainWindow 的 XAML 和代码与 README.MD 中的几乎完全相同

I also copied over those 2 files (libcef.dll and icudt.dll) from the 0.25.7 Binary package from github to the bin\Debug and bin\Release folders by hand.

我还手动将 0.25.7 二进制包中的这 2 个文件(libcef.dll 和 icudt.dll)从 github 复制到 bin\Debug 和 bin\Release 文件夹。

What am i doing wrong?

我究竟做错了什么?

采纳答案by jornh

Hmm, I realize this is a few months back and it lookslike the guide and code you applied was for the CefSharp1 code branch (that version did AFAIK only support x86). Notethe WPF control for CefSharp1 and current masteris quite different.

嗯,我意识到这是几个月前的事情,看起来您应用的指南和代码适用于 CefSharp1 代码分支(该版本 AFAIK 仅支持 x86)。注意CefSharp1 和 current 的 WPF 控件master是完全不同的。

With CefSharp 33.0.0 just released I would suggest you try with that version of the NuGet andthat you start out with getting everything running with the WPF exampleof CefSharp.MinimalExamplefirst. I think the guide you used has been changed a bit since then. Not sure if it's ready for prime time yet though.

随着CefSharp 33.0.0刚刚发布,我建议你尝试用该版本的NuGet的你在开始时让一切与正在运行的WPF例子CefSharp.MinimalExample第一。我认为您使用的指南从那时起已经发生了一些变化。不确定它是否已经准备好迎接黄金时段。

Finally there's a recent post on the CefSharp Google Group with a good write-up on the "DIY version of MinimalExample". Read the first two posts there which I think should still apply.

最后,最近在 CefSharp Google Group 上发表了一篇关于“MinimalExample 的 DIY 版本”的好文章。阅读那里的前两篇文章,我认为它们仍然适用。

回答by user3313608

I also started a collection of Hello World samples based on CefSharp 3 via NuGet libraries. Have a look over here if you need 101 type guidance on CefSharp in WPF:

我还通过 NuGet 库开始收集基于 CefSharp 3 的 Hello World 示例。如果您需要 WPF 中 CefSharp 的 101 类型指南,请查看此处:

https://github.com/Dirkster99/KB

https://github.com/Dirkster99/KB