我一定遗漏了一些明显的东西,但我不确定是什么。
我创建了一个空白的 C++ Metro 应用程序,并且刚刚添加了一个模型,我将在我的 UI 中绑定(bind)到该模型,但是我收到了一系列与 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;
}
如果我从 MyColor
中删除 Bindable
属性,代码将编译。
谁能告诉我我错过了什么非常明显的事情,这样我就可以给自己一个面子并解决问题?
最佳答案
我做了一个小改动,现在可以用了。
我加了
#include "MyColor.h"
到 BlankPage.xaml.h
文件,即使我还没有添加任何其他对 MyColor
的引用并且它现在可以编译。
我想如果你做了一些[Bindable]
标题必须包含在至少一个xaml页面中才能工作。未在任何地方引用的可绑定(bind)类型会导致编译器错误。
关于xamltypeinfo.g.cpp 中的 C++ 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814275/