【问题标题】:VC++ Converter is not a member of namespaceVC++ 转换器不是命名空间的成员
【发布时间】:2017-10-17 07:40:12
【问题描述】:

原问题

我正在尝试使我的 XAML 设计中的某些宽度依赖于其他宽度。为了实现这一点,我发现我需要使用IValueConverter 来实现绑定。几乎可以归结为this question(或者,更详细的帖子,click here)的方法。

我的代码相当简单,但它不断产生以下错误:error C2039: 'GridViewWidthConverter': is not a member of 'MyNamespace'。为什么?

现在,我的 XAML 代码的相关部分如下所示:

<Page
    ...
    xmlns:local="using:MyNamespace"
    ...>

    <!-- Horizontal scrolling grid -->
    <GridView>
        <GridView.Resources>
            <local:GridViewWidthConverter x:Key="GridViewWidthConverter" />
        </GridView.Resources>
    ...
    </GridView>
    ...
</Page>

GridViewWidthConverter.h:

#pragma once

#include "pch.h"

using namespace Platform;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Interop;


namespace MyNamespace
{
    public ref class GridViewWidthConverter sealed : IValueConverter
    {
    public:
        GridViewWidthConverter();
        virtual ~GridViewWidthConverter();
        virtual Object^ Convert(Object^ value, TypeName targetType, Object^ parameter, String^ language);
        virtual Object^ ConvertBack(Object^ value, TypeName targetType, Object^ parameter, String^ language);
    };
}

GridViewWidthConverter.cpp:

#include "pch.h"
#include "GridViewWidthConverter.h"

using namespace MyNamespace;

// Constructor
GridViewWidthConverter::GridViewWidthConverter(){}

// Destructor
GridViewWidthConverter::~GridViewWidthConverter(){}

Object ^ GridViewWidthConverter::Convert(Object ^ value, TypeName targetType, Object ^ parameter, String ^ language)
{
    return value;
}

Object ^ GridViewWidthConverter::ConvertBack(Object ^ value, TypeName targetType, Object ^ parameter, String ^ language)
{
    return value;
}

编辑

完整的错误说明如下:

Error C2039: 'GridViewWidthConverter': is not a member of 'MyNamespace' in \generated files\xamltypeinfo.g.cpp, line 200

Error C2065: 'GridViewWidthConverter': undeclared identifier in \generated files\xamltypeinfo.g.cpp, line 200

Error C2440: 'initializing': cannot convert from 'overloaded-function' to 'Platform::Object ^(__cdecl *)(void)' in \generated files\xamltypeinfo.g.cpp, line 252

非常感谢!

【问题讨论】:

  • 这个问题可能与您的包含文件的顺序有关。在使用类型转换器的 Page 实现的 .xaml.h 文件中,确保在生成的任何 .gh 之前包含类型转换器头文件文件。

标签: xaml visual-c++ namespaces uwp c++-cx


【解决方案1】:

对于那些仍在挣扎的人:

XamlTypeInfo.g.cpp 是一个生成的文件,其中包括:pch.h、App.xaml.h、App.g.hpp,以及每个 XAML 页面的 *.xaml.h 和 *.g.hpp 头文件在您的应用程序中。生成该文件后,它知道应用程序中的所有可绑定类型,以便在 XAML 解析器遇到它们时实例化它们。

如果您的转换器类型未以某种方式包含在这些头文件之一中,那么您将收到此错误。如果您在预编译的头文件、App.xaml.h 头文件或其他 *.xaml.h 头文件之一中#include 头文件,那么它应该被清除。 (生成 .g.hpp 文件,因此您对它们所做的任何更改都不会保留。)最好将它#include 到 pch.h 标头中,因为这样在所有 CPP 文件和将避免编译 PCH 本身的任何问题。

来源:link

【讨论】:

    【解决方案2】:

    在标头命名空间中,名称被声明为 namespace MyNamespace;,但在 cpp 文件中,它是 using namespace MyNameSpace;(注意大小写差异)。 C++ 和 C++/Cx 都是区分大小写的语言,因此 MyNamespaceMyNameSpace 是两个不同的标识符。

    您的标头还包括预编译标头并具有全局 using 指令。

    【讨论】:

    • 您对大小写差异的看法是对的,那是我的代码中的一个错字,我已经修复了,但在这里错误地复制了。我已经从上面的代码中编辑了这些错误。不用说,错误仍然存​​在。包含 pch.h 和使用全局 using 指令到底有什么问题?
    • 我已经在我的问题中包含了详细信息。错误出现在生成的xamltypeinfo.g.cpp文件中。
    • pch.h 可能是也可能不是预编译头文件。光看代码是无法判断的。即使是这样,using 语句对于它出现的编译单元也是本地的(在这种情况下为 GridViewWidthConverter.cpp)。您认为有哪些负面影响?
    【解决方案3】:

    我花了一些时间,但我通过删除导致错误的 XAML 代码、构建项目、重新构建项目、重新添加代码并开始新的构建或调试会话来解决我的问题。 Stack Overflow 上有很多这样的“解决方案”,但是,我仍然不清楚这个问题的真正原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多