名称空间 "clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" 中不存在名称 ChromiumWebBrowser

The name ChromiumWebBrowser does not exist in the namespace "clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"

我按照这个 link 来实现 CefSharp 应用程序。

但是我在编码时卡住了 MainWindow.xaml

Blend for VS 2015 说,

The name ChromiumWebBrowser does not exist in the namespace "clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"

但我安装了 CefSharp.Common 和 CefSharp.Wpf v51.0.0、cef.redist.x64 和 cef.redist.x86 v3.2704.1432 以及 NuGet 包管理器。

我是 C# 开发新手,所以我不知道如何解决这个问题。请帮我解决这个错误。

这是我的MainWindow.xaml

<Window x:Class="StocktalkBrowser.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:StocktalkBrowser"
        xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <cefSharp:ChromiumWebBrowser Grid.Row="0"  Address="https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions" />
    </Grid>
</Window>

我刚刚尝试了 link,正如我在评论中提到的那样,它编译没有任何问题。

可以忽略。

而且预览也不可用,我学会了忍受它。

编译时没有错误

然后程序启动。没问题

对我来说,问题是解决方案平台设置为 AnyCPU。
据我所知,CefSharp 不支持 AnyCPU。有趣的是解决方案 运行 在发布模式下很好,但在调试模式下我立即得到一个错误 - the invocation of the constructor on type that matches ... the specified binding constraints threw an exception.

一旦我将解决方案平台更改为仅 x64,XAML 错误就消失了 (The name ChromiumWebBrowser does not exist in the namespace “clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf”)。我可以 运行 在调试和发布模式下的解决方案。

XAML 设计器不支持它,因此请在 运行 时间加载它:

  1. 从 XAMl 中删除元素,然后放置任何容器,例如边框:

    <Border x:Name="cefChromeContainer" />
    
  2. 在Constructor代码中,InitializeComponent();调用后,生成浏览器元素并放入容器中。最好在 class 独家新闻中声明浏览器:

    CefSharp.Wpf.ChromiumWebBrowser browser = new CefSharp.Wpf.ChromiumWebBrowser();
    
    public MainWindow()
    {
        InitializeComponent();
        cefChromeContainer.Content = browser;
        browser.Address = "https://whosebug.com";
    }
    

你可以在NuGet包的(Documentation)中阅读,还需要在你的应用程序中添加一个app.manifest

我遇到了同样的问题,我解决了将配置从“debug”更改为“release”并使用“x64" 作为平台。

但是从解决方案菜单中更改它是不够的,就像我通常做的那样。您需要转到“编译 > 配置管理”下的 visual studio 主菜单,您可以在下面的 link 中看到 Microsoft:

https://docs.microsoft.com/es-es/visualstudio/ide/how-to-configure-projects-to-target-platforms?view=vs-2015&redirectedfrom=MSDN#Anchor_0

这个解决方案实际上在 cefsharp 的 Readme.txt 文件中,如 saschad 在他的 "Documentation" link 中提供的答案:https://github.com/cefsharp/CefSharp/blob/cefsharp/69/NuGet/Readme.txt

在那里你可以阅读它:

现在我拥有了它 运行 就像一个魅力 :D