0

I have a Visual Studio 2019 solution that contains two project.

The first project has been created using the "Windows Runtime Component (WinUI 3) Visual Studio template (library project). It generates a dll and associated winmd file that contains a DirectX-based Windows Runtime component to be used in the user interface of a WinUI 3.0 app.

The second project has been created using the "Blank App, Packaged (WinUI 3 in Desktop)" Visual Studio template. It generates a Win32 app that utilizes WinUI 3.0 as the UI framework and consumes the output of the first project.

That second project references the first project via Visual Studio / MSBuild project reference.

The first project defines the component via MIDL as follows:

namespace My.Company.Name.DirectX
{
  runtimeclass CustomControl : Microsoft.UI.Xaml.Controls.SwapChainPanel
  {
    CustomControl();
    // All interface methods and properties
  }
}

The header that gets generated form this looks as follows:

namespace winrt::My::Company::Name::DirectX::implementation
{
  struct CustomControl : CustomControlT<CustomControl>
  {
  public:
    CustomControl();
    ~CustomControl();
    // // All interface methods and properties
  };
}

namespace winrt::My::Company::Name::DirectX::factory_implementation
{
  struct CustomControl : CustomControlT<CustomControl, implementation::CustomControl>
  {
  };
}

I then attempt to use that component within a Microsoft.UI.Xaml.Controls.Page XAML file as follows:

<Page
  x:Class="My.Company.Name.App.CustomControlPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:My.Company.Name.App"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:Controls="using:My.Company.Name.DirectX"
  mc:Ignorable="d">

  <Controls:CustomControl/>

</Page>

However, if I compile the entire code I get the following error messages:

My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(29,46): error C3083: 'DirectX': the symbol to the left of a '::' must be a type (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(145): message : see reference to class template instantiation 'winrt::My::Company::Name::App::implementation::CustomControlPageT<D,I...>' being compiled (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(29,55): error C2039: 'CustomControl': is not a member of 'winrt::My::Company::Name' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(10): message : see declaration of 'winrt::My::Company::Name' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(29,66): error C3646: 'CustomControl': unknown override specifier (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(29,76): error C2059: syntax error: '(' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(30,1): error C2334: unexpected token(s) preceding '{'; skipping apparent function body (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(33,62): error C3083: 'DirectX': the symbol to the left of a '::' must be a type (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(33,71): error C2039: 'CustomControl': is not a member of 'winrt::My::Company::Name' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(10): message : see declaration of 'winrt::My::Company::Name' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(33,1): error C2061: syntax error: identifier 'CustomControl' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(134,46): error C3083: 'DirectX': the symbol to the left of a '::' must be a type (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(134,55): error C2039: 'CustomControl': is not a member of 'winrt::My::Company::Name' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(10): message : see declaration of 'winrt::My::Company::Name' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(134,66): error C3646: '_CustomControl': unknown override specifier (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(134,77): error C2059: syntax error: '{' (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(134,1): error C2334: unexpected token(s) preceding '{'; skipping apparent function body (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(35,13): error C3861: '_CustomControl': identifier not found (compiling source file Page\CustomControlPage.cpp)
My.Company.Name.App\Generated Files\Page\CustomControlPage.xaml.g.h(35,27): error C3861: 'value': identifier not found (compiling source file Page\CustomControlPage.cpp)

Somehow, my CustomControl is not recognized when compiling the source code that is generated for the XAML file that references it. I don't understand what the problem is. In theory, all I that is required to use that component is the winmd file because it should describe that component.

Can someone tell me why I'm getting these errors?

Edit:

I have attempted to reproduce the problem and the resulting Git repository is available here

ackh
  • 1,648
  • 2
  • 18
  • 36
  • Is the consuming project referencing the Windows Runtime Component project? See [Windows Runtime components with C++/WinRT](https://learn.microsoft.com/en-us/windows/uwp/winrt-components/create-a-windows-runtime-component-in-cppwinrt) for details. – IInspectable Jan 11 '22 at 13:30
  • Good point, I didn't mention this initially but have added a short statement now. The second (app) project references the first (library) project via Visual Studio / MSBuild project reference. I have also tried to reference the winmd file directly in the second project but that resulted in the exact same compilation errors. – ackh Jan 11 '22 at 13:41
  • Does your custom `Page`-derived implementation include the respective (generated) header files (presumably ``)? – IInspectable Jan 11 '22 at 13:47
  • Are you referring to the `CustomControl.g.h` file that gets created in the `Generated Files` folder of the library project by C++/WinRT? If so, the answer is no, that file is not part of any `#include` statement in the `Page` files of the app project. Would that need to be the case? My understanding is that C++/WinRT should get all required information about `CustomControl` from the winmd file. – ackh Jan 11 '22 at 14:08
  • 2
    The errors are emitted when compiling `CustomControlPage.cpp`. That file (or its corresponding header) needs to include the generated projected header file(s), such as ``. A [mcve] would be helpful to see where things do go wrong. – IInspectable Jan 11 '22 at 14:29
  • 1
    I have created a [Git repository](https://github.com/ackh/My.Company.Name.App) that reproduces the problem. Help with it is appreciated. – ackh Jan 11 '22 at 18:48
  • 2
    Alright, you're correct @IInspectable. What was missing was the include statement for the header file containing the projected library types. I now added `#include "Generated Files/winrt/My.Company.Name.DirectX.h"` to the `pch.h`. If you post this as the answer I gladly accept it. – ackh Jan 13 '22 at 10:03

0 Answers0