xamltypeinfo.g.cpp 中的 C++ 编译器错误

标签 c++ xaml binding microsoft-metro c++-cx

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

我创建了一个空白的 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/

相关文章:

c++ - 尝试在 Ubuntu Linux 中构建全局键盘 Hook 时出错

c++ - 有没有一种方法可以自动链接 OpenGL 程序需要的所有库,而无需在编译时明确写入它们的标志?

windows - 从 Windows 应用商店应用程序的超链接中删除下划线

c# - 单击按钮后更改按钮的效果 WPF

c++ - 类局部变量的指针?

C++ 返回一个计数

wpf - 如何防止 itemsControl 项目内容拉伸(stretch)到最宽项目的宽度

wpf - 为什么绑定(bind)对 ICommand 属性进行特殊处理?

javascript - Angular : displaying elements in navbar after logged in

java - Spring MVC - 提交后子实体丢失