无法将运行时类绑定到XAML T必须是WinRT类型

问题描述 投票:0回答:1

我正在尝试做一个c ++ / winrt项目,以学习更多有关c ++的知识。和Windows开发我使用的c ++ / winrt版本是2.0.200117.5我要使用以下课程将IObservableVector绑定到xaml ui

BrushSizeModel.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);
    }
}

BrushDataModel.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()
    {
    }
}

[当我编译时,出现以下错误,我尝试用google搜索看看我对课堂的了解中缺少了什么最终从winrt类型派生,但这在各地都有在线专家。如果有种子,有人可以帮忙播种吗我的代码有问题吗?

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 ==========
c++ winrt-xaml c++-winrt
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.