C++ compiler errors in xamltypeinfo.g.cpp
Asked Answered
F

1

9

I must be missing something obvious but I'm not sure what.

I've created a blank C++ metro app and I've just added a model that I will bind to in my UI however I'm getting a range of compiler errors related to xamltypeinfo.g.cpp and I'm not sure what I've missed.

My header file looks like this:

#pragma once
#include "pch.h"
#include "MyColor.h"

using namespace Platform;

namespace CppDataBinding
{
    [Windows::UI::Xaml::Data::Bindable]
    public ref class MyColor sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    public:
        MyColor();
        ~MyColor();

        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;
        property Platform::String^ RedValue
        {
            Platform::String^ get()
            { 
                return _redValue;
            }
            void set(Platform::String^ value)
            {
                _redValue = value;
                RaisePropertyChanged("RedValue");
            }
        }

    protected:
        void RaisePropertyChanged(Platform::String^ name);

    private:
        Platform::String^ _redValue;
    };
}

and my cpp file looks like this:

#include "pch.h"
#include "MyColor.h"

using namespace CppDataBinding;

MyColor::MyColor()
{
}

MyColor::~MyColor()
{
}

void MyColor::RaisePropertyChanged(Platform::String^ name)
{
    if (PropertyChanged != nullptr)
    {
        PropertyChanged(this, ref new  Windows::UI::Xaml::Data::PropertyChangedEventArgs(name));
    }
}

Nothing too tricky, but when I compile I get errors in xamltypeinfo.g.cpp indicating that MyColor is not defined in CppDataBinding.

The relevant generated code looks like this:

if (typeName == "CppDataBinding.MyColor")
{
    userType = ref new XamlUserType(this, typeName, GetXamlTypeByName("Object"));
    userType->Activator = ref new XamlTypeInfo::InfoProvider::Activator(
                            []() -> Platform::Object^ 
                            { 
                                return ref new CppDataBinding::MyColor(); 
                            });
    userType->AddMemberName("RedValue", "CppDataBinding.MyColor.RedValue");
    userType->SetIsBindable();
    xamlType = userType;
}

If I remove the Bindable attribute from MyColor the code compiles.

Can someone tell me what blindingly obvious thing I've missed so I can give myself a facepalm and fix the problem?

Firooc answered 21/3, 2012 at 23:21 Comment(12)
One blindingly obvious thing you've apparently missed is that whatever you're using here, it's not C++.Rufusrug
What error messages does the compiler emit?Bushy
what does this xor "^" do "Platform::String^" here?Crowl
@g24l: This is C++/CX. The ^ is a hat and declares a reference-counting smart pointer.Bushy
@g24l: There called tracking references.Loosejointed
@Jesse: While the ^ is a tracking reference in C++/CLI, that's not what it is in C++/CX. There's nothing to track in C++/CX because objects do not move (i.e., there's no compacting garbage collector).Bushy
@JamesMcNellis: Thanks for the insight, I haven't kept up with the times.Loosejointed
Jesse and JamesMcNellis , thanks :)Crowl
@JamesMcNellis First error is C2039: 'MyColor' : is not a member of 'CppDataBinding'.Firooc
Actually. I had exactly the same situation as yours and it also compiles if you include your header in App.xaml.h file.Helium
@JesseGood u mad, bro?Bicycle
@McGarnagle: Not sure what you are referring to.Loosejointed
F
15

I made one small change and now it works.

I added

#include "MyColor.h"

to the BlankPage.xaml.h file, even though I haven't yet added any other references to MyColor and it now compiles.

I guess if you make something [Bindable] the header must be included in at least one xaml page for it to work. A bindable type that is not referenced anywhere causes compiler errors.

Firooc answered 22/3, 2012 at 5:36 Comment(5)
Ah, yes. Your analysis is correct. This is a limitation of the XAML compiler.Bushy
I agree, this is definitely the case. I think in terms of cleanliness putting it in App.xaml.h might make more sense, seems like a good place for global-esque includes.Petree
What if you're project is a custom control that doesn't have any *.xaml.h? So far polluting the pch.h is the way I can think of to make it work.Biophysics
You should still have an App.xaml.hWyatt
@Tim You should NOT change pch.h frequently. It is for the precompiled header, presumably to allow C++ compiler to process macro faster. Every time you change it, VS rebuild the header which can be as big as 100MB, significantly defeat the original purpose.Porta

© 2022 - 2024 — McMap. All rights reserved.