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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With