无法创建运行时类



我正在使用Visual Studio 2022 17.4.0 Preview 2.1、WinUI 3以及WindowsAppSDK 1.1.5和C++/WinRT 2.0.220929.3,并希望使用模板创建ListView。在这个目标中,我需要创建一个DataType,它将被模板使用,但无法编译

联系人.idl

namespace Pine
{
[default_interface]
runtimeclass Contact
{
Contact();
}
}

联系人.h

#pragma once
#include "Contact.g.h"
namespace winrt::Pine::implementation
{
struct Contact : ContactT<Contact>
{
Contact();
};
}
namespace winrt::Pine::factory_implementation
{
struct Contact : ContactT<Contact, implementation::Contact>
{
};
}

联系人.cpp

#include "pch.h"
#include "Contact.h"
#if __has_include("Contact.g.cpp")
#include "Contact.g.cpp"
#endif
using namespace winrt;
using namespace Microsoft::UI::Xaml;
namespace winrt::Pine::implementation
{
Contact::Contact()
{
}
}

这些文件似乎对应于我见过的所有其他运行时类,但它会产生两个链接错误:

1>Contact.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl winrt::Pine::implementation::ContactT<struct winrt::Pine::implementation::Contact>::Connect(int,struct winrt::Windows::Foundation::IInspectable const &)" (?Connect@?$ContactT@UContact@implementation@Pine@winrt@@$$V@implementation@Pine@winrt@@UEAAXHAEBUIInspectable@Foundation@Windows@4@@Z)
1>Contact.obj : error LNK2001: unresolved external symbol "public: virtual struct winrt::Microsoft::UI::Xaml::Markup::IComponentConnector __cdecl winrt::Pine::implementation::ContactT<struct winrt::Pine::implementation::Contact>::GetBindingConnector(int,struct winrt::Windows::Foundation::IInspectable const &)" (?GetBindingConnector@?$ContactT@UContact@implementation@Pine@winrt@@$$V@implementation@Pine@winrt@@UEAA?AUIComponentConnector@Markup@Xaml@UI@Microsoft@4@HAEBUIInspectable@Foundation@Windows@4@@Z)
1>C:UsersusersourcereposPinex64DebugPinePine.exe : fatal error LNK1120: 2 unresolved externals

我想创建一个类,我将能够在Xaml文件中以以下方式使用它:

<ListView>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Contact">
...
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

谢谢你抽出时间。

生成的文件Contact.g.h.中有一个错误

由于某些原因,defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h")为true(未定义宏且文件不存在(,因此在文件末尾使用了错误的代码。我不得不删除下面的这些行。

//#if defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h")
//
//#include "Contact.xaml.g.h"
//
//#else
namespace winrt::Pine::implementation
{
template <typename D, typename... I>
using ContactT = Contact_base<D, I...>;
}
//#endif

最新更新