Can't create a runtime class

236 Views Asked by At

I'm using Visual Studio 2022 17.4.0 Preview 2.1, WinUI 3 with the WindowsAppSDK 1.1.5 and C++/WinRT 2.0.220929.3, and want to create a ListView using a template. In this goal I need to create a DataType which will be used by the template but can't compile it. I created the 3 following files:

Contact.idl

namespace Pine
{
    [default_interface]
    runtimeclass Contact
    {
        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>
    {
    };
}

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

These files seem to correspond to every other runtimeclass I have seen, however it generates two linking errors:

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:\Users\user\source\repos\Pine\x64\Debug\Pine\Pine.exe : fatal error LNK1120: 2 unresolved externals

I want to create a Class which I'll be able to use in the following way in Xaml files:

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

Thank you for your time.

1

There are 1 best solutions below

5
Jacques On

There is a bug in the generated file Contact.g.h.

For some reasons, defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h") is true (the macro is not defined and the files does not exist) so the wrong code is used at the end of the file. I had to delete these lines below.

//#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