gpt4 book ai didi

c++ - 无法将运行时类绑定(bind)到 XAML T 必须是 WinRT 类型

转载 作者:行者123 更新时间:2023-12-02 10:06:41 35 4
gpt4 key购买 nike

I'm trying to do a c++/winrt project to learn a little bit more of c++ and windows development The version of c++/winrt which I', using is 2.0.200117.5 I have the following classes which I want to use to bind an IObservableVector to the xaml ui



画笔大小模型
.idl
namespace DrawingCanvas
{
[bindable]
[default_interface]
runtimeclass BrushSizeModel : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
BrushSizeModel();
BrushSizeModel(Single width, Single height);
Single Width;
Single Height;
}
}

。H
    #pragma once

#include "BrushSizeModel.g.h"

namespace winrt::DrawingCanvas::implementation
{
struct BrushSizeModel : BrushSizeModelT<BrushSizeModel>
{
BrushSizeModel() = default;

BrushSizeModel(float width, float height);

float Width();
void Width(float value);

float Height();
void Height(float value);

winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& value);
void PropertyChanged(winrt::event_token const& token);

private:
float m_width;
float m_height;

winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
};
}

namespace winrt::DrawingCanvas::factory_implementation
{
struct BrushSizeModel : BrushSizeModelT<BrushSizeModel, implementation::BrushSizeModel>
{
};
}

.cpp
#include "pch.h"
#include "BrushSizeModel.h"
#if __has_include("BrushSizeModel.g.cpp")
#include "BrushSizeModel.g.cpp"
#endif

namespace winrt::DrawingCanvas::implementation
{
BrushSizeModel::BrushSizeModel(float width, float height)
:m_width(width),
m_height(height)
{
}

float BrushSizeModel::Width()
{
return m_width;
}

void BrushSizeModel::Width(float value)
{
m_width = value;
}

float BrushSizeModel::Height()
{
return m_height;
}

void BrushSizeModel::Height(float value)
{
m_height = value;
}

winrt::event_token BrushSizeModel::PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return m_propertyChanged.add(handler);
}

void BrushSizeModel::PropertyChanged(winrt::event_token const& token)
{
m_propertyChanged.remove(token);
}
}

刷数据模型
。ID
import "BrushSizeModel.idl";

namespace DrawingCanvas
{
[bindable]
[default_interface]
runtimeclass BrushDataModel : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
BrushDataModel();
BrushDataModel(
String id,
String name,
BrushSizeModel size,
Windows.UI.Input.Inking.PenTipShape penTipShape
);

String Id;
String Name;
BrushSizeModel Size;
Windows.UI.Input.Inking.PenTipShape PenTipShape;
}
}

。H
#pragma once

#include "BrushDataModel.g.h"
#include "BrushSizeModel.g.h"

namespace winrt::DrawingCanvas::implementation
{
struct BrushDataModel : BrushDataModelT<BrushDataModel>
{
BrushDataModel();

BrushDataModel(
hstring id,
hstring name,
BrushSizeModel size,
Windows::UI::Input::Inking::PenTipShape penTipShape
);

hstring Id();
void Id(hstring value);

hstring Name();
void Name(hstring value);

BrushSizeModel Size();
void Size(BrushSizeModel value);

Windows::UI::Input::Inking::PenTipShape PenTipShape();
void PenTipShape(Windows::UI::Input::Inking::PenTipShape value);

winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& value);

void PropertyChanged(winrt::event_token const& token);

private:
static Windows::UI::Xaml::DependencyProperty m_IsMovableProperty;

hstring m_id;
hstring m_name;
BrushSizeModel m_size;
Windows::UI::Input::Inking::PenTipShape m_penTipShape;

winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
};
}

namespace winrt::DrawingCanvas::factory_implementation
{
struct BrushDataModel : BrushDataModelT<BrushDataModel, implementation::BrushDataModel>
{
};
}

.cpp
#include "pch.h"
#include "BrushDataModel.h"
#if __has_include("BrushDataModel.g.cpp")
#include "BrushDataModel.g.cpp"
#endif

namespace winrt::DrawingCanvas::implementation
{
BrushDataModel::BrushDataModel()
: m_id(L"NO_ID"),
m_name(L"NO_NAME"),
m_size(BrushSizeModel()),
m_penTipShape(Windows::UI::Input::Inking::PenTipShape::Circle)
{
}

BrushDataModel::BrushDataModel(hstring id, hstring name, BrushSizeModel size, Windows::UI::Input::Inking::PenTipShape penTipShape)
: m_id(id),
m_name(name),
m_size(size),
m_penTipShape(penTipShape)
{
}
hstring BrushDataModel::Id()
{
return m_id;
}
void BrushDataModel::Id(hstring value)
{
m_id = value;
}
hstring BrushDataModel::Name()
{
return m_name;
}
void BrushDataModel::Name(hstring value)
{
m_name = value;
}
BrushSizeModel BrushDataModel::Size()
{
return m_size;
}
void BrushDataModel::Size(BrushSizeModel value)
{
m_size = value;
}
Windows::UI::Input::Inking::PenTipShape BrushDataModel::PenTipShape()
{
return m_penTipShape;
}
void BrushDataModel::PenTipShape(Windows::UI::Input::Inking::PenTipShape value)
{
m_penTipShape = value;
}

winrt::event_token BrushDataModel::PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return m_propertyChanged.add(handler);
}

void BrushDataModel::PropertyChanged(winrt::event_token const& token)
{
m_propertyChanged.remove(token);
}
}

BrushCreatorViewModel
.idl
import "BrushDataModel.idl";

namespace DrawingCanvas
{
[bindable]
[default_interface]
runtimeclass BrushCreatorViewModel : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
BrushCreatorViewModel();

Windows.Foundation.Collections.IObservableVector<BrushDataModel> VmFilteredBrushes{ get; };

String VmFilter;
}
}

。H
#pragma once

#include "BrushCreatorViewModel.g.h"
#include "BrushDataModel.h"
#include <vector>

namespace winrt::DrawingCanvas::implementation
{
struct BrushCreatorViewModel : BrushCreatorViewModelT<BrushCreatorViewModel>
{
BrushCreatorViewModel();

winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& value);

void PropertyChanged(winrt::event_token const& token);

Windows::Foundation::Collections::IObservableVector<BrushDataModel> VmFilteredBrushes();

hstring VmFilter();

void VmFilter(hstring value);

private:
winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;

Windows::Foundation::Collections::IObservableVector<BrushDataModel> m_filteredBrushes;

std::vector<BrushDataModel> m_allBrushesList;

hstring m_filter;

private:
void ApplyFilter();
};
}

namespace winrt::DrawingCanvas::factory_implementation
{
struct BrushCreatorViewModel : BrushCreatorViewModelT<BrushCreatorViewModel, implementation::BrushCreatorViewModel>
{
};
}

.cpp
#include "pch.h"
#include "BrushCreatorViewModel.h"
#if __has_include("BrushCreatorViewModel.g.cpp")
#include "BrushCreatorViewModel.g.cpp"
#endif

namespace winrt::DrawingCanvas::implementation
{
BrushCreatorViewModel::BrushCreatorViewModel()
:m_allBrushesList(),
m_filter(L"")
{
m_filteredBrushes = winrt::single_threaded_observable_vector<BrushDataModel>();
}

winrt::event_token BrushCreatorViewModel::PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return m_propertyChanged.add(handler);
}

void BrushCreatorViewModel::PropertyChanged(winrt::event_token const& token)
{
m_propertyChanged.remove(token);
}
Windows::Foundation::Collections::IObservableVector<BrushDataModel> BrushCreatorViewModel::VmFilteredBrushes()
{
return m_filteredBrushes;
}
hstring BrushCreatorViewModel::VmFilter()
{
return m_filter;
}
void BrushCreatorViewModel::VmFilter(hstring value)
{
m_filter = value;
m_propertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"VmFilter" });
}
void BrushCreatorViewModel::ApplyFilter()
{
}
}

When I compile I get the following error which I tryed to google to see what am I missing from what I understood my classes must ultimately derive from a winrt type but this is not present in the online exaples anywhere. Can anybody help if to seed if there is a problem in my code ?


1>Validating metadata file Debug\Merged\DrawingCanvas.winmd.
1>BrushCreatorView.cpp
1>BrushCreatorViewModel.cpp
1>BrushDataModel.cpp
1>BrushSizeModel.cpp
1>App.cpp
1>MainPage.cpp
1>XamlTypeInfo.Impl.g.cpp
1>D:\Workspace\Organiztions\home\visual-cpp\DrawingCanvas\DrawingCanvas\Generated Files\winrt\impl\Windows.Foundation.Collections.1.h(90,29): error C2338: T must be WinRT type. (compiling source file BrushCreatorViewModel.cpp)
1>D:\Workspace\Organiztions\home\visual-cpp\DrawingCanvas\DrawingCanvas\BrushCreatorViewModel.h(26): message : see reference to class template instantiation 'winrt::Windows::Foundation::Collections::IObservableVector<winrt::DrawingCanvas::implementation::BrushDataModel>' being compiled (compiling source file BrushCreatorViewModel.cpp)
1>Done building project "DrawingCanvas.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

在您的 BrushCreatorViewModel实现您正在尝试创建 BrushDataModel 的 WinRT 集合,但您使用的是 BrushDataModel实现类型作为类型参数而不是 BrushDataModel投影类型。使用后者来避免此错误。

关于c++ - 无法将运行时类绑定(bind)到 XAML T 必须是 WinRT 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59907570/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com