【问题标题】:How to use CefSharp for WPF properly?如何正确使用 CefSharp for WPF?
【发布时间】:2014-09-19 21:11:58
【问题描述】:

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

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

遗憾的是,我遇到了 4 个错误,但我不知道如何摆脱它们!

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

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

我的主窗口 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>

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;
            }
        }
    }
}

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

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

我做错了什么?

【问题讨论】:

  • 我通过将目标框架设置为 4.0 并将平台目标设置为 x64 将其降低为 1 个错误。仍然认为这个错误:错误3名称“WebView”在命名空间“clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf”中不存在。 “文件路径”\Chromium\MainWindow.xaml 6 9 Chromium
  • 嗯,我意识到这是几个月前的事了,它看起来就像您应用的指南和代码是用于 CefSharp1 代码分支的。

标签: wpf xaml c#-4.0 visual-studio-2012 cefsharp


【解决方案1】:

嗯,我意识到这是几个月前的事了,它看起来就像您应用的指南和代码是针对 CefSharp1 代码分支的(AFAIK 的那个版本只支持 x86)。 注意 CefSharp1 和当前master 的WPF 控件完全不同。

在刚刚发布的 CefSharp 33.0.0 中,我建议您尝试使用 NuGet 的那个版本并且,您首先使用 WPF exampleCefSharp.MinimalExample 开始运行所有内容。我认为你使用的指南从那时起已经改变了一点。不确定它是否已准备好迎接黄金时段。

最后,在 CefSharp Google Group 上发布了 a good write-up on the "DIY version of MinimalExample" 的最新帖子。阅读我认为仍然适用的前两篇文章。

【讨论】:

  • 现在 意识到这是几个月前的事了,但我在使用 CefSharp 39.0.1 和 CefSharp.MinimalExample 以及我自己的代码时遇到了同样的问题.但是,如果我在 .cs 中手动创建 ChromiumWebBrowser,那就没问题了。只有当我(或 MinimalExample)尝试在 XAML 中执行此操作时,才会出现错误。
  • 您具体遇到了什么错误?如果是The name "XXXX" does not exist in the namespace "clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf",可以在开始调试前关闭XAML文件来摆脱它吗?
  • 啊,确实摆脱了它。它从来没有真正阻止构建的发生,只是错误地声称错误。抱歉,对这个 .NET 东西有点新,但仍然不习惯“不是真正错误的错误”位。 :O
  • 不,这真的很混乱!我认为github.com/cefsharp/CefSharp/wiki/… 和论坛帖子链接指向可能的潜在问题。如果您能想出一个好的短语,请随意扩展常见问题解答,因为它是一个 wiki 页面,任何拥有 GitHub 帐户的人都可以编辑。
猜你喜欢
  • 2019-07-06
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2013-06-28
  • 2020-10-11
  • 1970-01-01
  • 2012-01-19
相关资源
最近更新 更多