1

Original question

I'm trying to make some widths in my XAML design dependent on other widths. In order to achieve this, I figured out I need to implement a binding using an IValueConverter. Pretty much boils down to the approach from this question (or, for a more detailed post, click here).

My code is fairly simple, but it keeps yielding the following error: error C2039: 'GridViewWidthConverter': is not a member of 'MyNamespace'. Why?

Now, the relevant part of my XAML code looks like this:

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

Edits

The full error specification is as follows:

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

Thanks a lot!

Community
  • 1
  • 1
Cas Dekkers
  • 372
  • 2
  • 12
  • The issue is probably related to the order of your include files. In the *.xaml.h* file for your `Page` implementation that uses the type converter, make sure to include the type converter header file prior to any of the generated *.g.h* files. – IInspectable May 20 '17 at 10:12

3 Answers3

1

In header namespace name is declared as namespace MyNamespace; but in cpp file it is using namespace MyNameSpace; (notice case difference). Both C++ and C++/Cx are case-sensitive languages so MyNamespace and MyNameSpace are two different identifiers.

Also your header includes precompiled header and has global using directives.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • You are right about the case difference, that was a typo in my code, which I had already fixed, but erroneously copied here. I have edited these mistakes out of the above code. Needless to say that the error still persists. What exactly is wrong about including `pch.h` and using global `using` directives? – Cas Dekkers May 18 '17 at 11:27
  • I've included the details in my question. The error occurs in the generated `xamltypeinfo.g.cpp` file. – Cas Dekkers May 18 '17 at 11:37
  • *pch.h* may or may not be a precompiled header. It's impossible to tell just by looking at the code. Even if it is, the `using` statement is local to the compilation unit where it appears (*GridViewWidthConverter.cpp* in this case). What negative effects do you have in mind? – IInspectable May 20 '17 at 10:06
1

For those who are still struggling:

XamlTypeInfo.g.cpp is a generated file that will include: pch.h, App.xaml.h, App.g.hpp, and *.xaml.h and *.g.hpp header files for each XAML page in your app. The file is generated so that it knows all of the bindable types in your application so that they can be instantiated whenever the XAML parser comes across them.

If your converter type isn't somehow included in one of those header files then you will get this error. If you #include your header file in the pre-compiled header, the App.xaml.h header, or one of the other *.xaml.h header files then it should clear up. (The .g.hpp files are generated so any changes you make to them wouldn't survive.) It's probably best just to #include it in the pch.h header since then the type will be known and accessible in all CPP files and will avoid any issues with compiling the PCH itself.

Source: link

Borzh
  • 5,069
  • 2
  • 48
  • 64
0

It took me a while, but I solved my problem by consequently removing the XAML code that causes the error, building the project, rebuilding the project, adding the code back in and starting a new build or debug session. There's lots of "solutions" like this on Stack Overflow, however, the true cause of this problem is still unclear to me.

Cas Dekkers
  • 372
  • 2
  • 12