9

我一定遗漏了一些明显的东西,但我不确定是什么。

我创建了一个空白的 C++ Metro 应用程序,并且刚刚添加了一个模型,我将在我的 UI 中绑定到该模型,但是我得到了一系列相关的编译器错误xamltypeinfo.g.cpp,我不确定我错过了什么。

我的头文件如下所示:

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

我的 cpp 文件如下所示:

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

没什么太棘手的,但是当我编译时,我得到错误,xamltypeinfo.g.cpp表明它MyColor没有在CppDataBinding.

相关的生成代码如下所示:

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

如果我从代码编译中删除该Bindable属性。MyColor

有人能告诉我我错过了什么明显的事情,这样我就可以给自己一个面子并解决问题吗?

4

1 回答 1

15

我做了一个小改动,现在它可以工作了。

我添加了

#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.

于 2012-03-22T05:36:49.237 回答