0

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.

Jacques
  • 140
  • 1
  • 10

1 Answers1

0

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
Jacques
  • 140
  • 1
  • 10
  • That's unlikely to be the solution. Heed the comment at the top of the file you just edited: `// WARNING: Please don't edit this file. It was generated by C++/WinRT v...`. Your edits will get reverted the next time this file is generated. – IInspectable Oct 04 '22 at 09:25
  • 1
    That said, the *actual* problem is that the generated implementation isn't getting compiled. It is in *Contact.xaml.g.hpp*, and gets automatically included by *XamlTypeInfo.g.cpp* residing in the same directory. To diagnose this issue find out why *XamlTypeInfo.g.cpp* doesn't get compiled for your project configuration. – IInspectable Oct 04 '22 at 09:52
  • I think that the issue comes from a previously generated *Contact.xaml.g.hpp* which hasn't been deleted when I deleted the corresponding files from the Solution Explorer. – Jacques Oct 04 '22 at 10:54
  • 1
    If you can get back to a state where things are failing to link, try *Build -> Clean Solution* followed by a build, or *Build -> Rebuild Solution*. That's likely the real solution here. – IInspectable Oct 04 '22 at 16:17
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 07 '22 at 15:09