Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++/WinRT - XAML control in a sub-folder doesn't work out of the box #7652

Open
2 tasks done
JaiganeshKumaran opened this issue Aug 26, 2022 · 10 comments
Open
2 tasks done
Labels
bug Something isn't working feature proposal New feature proposal needs-review 👀 product-winui3 WinUI 3 issues team-Markup Issue for the Markup team

Comments

@JaiganeshKumaran
Copy link
Contributor

Describe the bug

When you keep XAML controls in a sub-folder, the project doesn't compile if you attempt to call, from the associated code behind file, any XAML-generated member function (eg: InitializeComponent or an element with x:Name). Specifically, this is caused as XAML Compiler saves the generated file(s) for the control inside a sub-folder under "Generated Files", but the header created by C++/WinRT assumes it's in the root of "Generated Files".

SettingsDialog.g.h from my project:

#if defined(WINRT_FORCE_INCLUDE_SETTINGSDIALOG_XAML_G_H) || __has_include("SettingsDialog.xaml.g.h")
#include "SettingsDialog.xaml.g.h"
#else

namespace winrt::PowerDevelop::implementation
{
    template <typename D, typename... I>
    using SettingsDialogT = SettingsDialog_base<D, I...>;
}

#endif

In this, the header doesn't include SettingsDialog.xaml.g.h as it's not there in the "Generated Files" root where SettingsDialog.g.h is present.

Steps to reproduce the bug

  1. Create a new C++/WinRT XAML app.
  2. Create a sub-folder within the project folder.
  3. Create a XAML control within the sub-folder.
  4. Try accessing any XAML-generated member function from code behind.
  5. See that the compiler doesn't recognise those.

Expected behavior

You should be able to keep XAML control in a sub-folder.

Screenshots

No response

NuGet package version

No response

Windows app type

  • UWP
  • Win32

Device form factor

No response

Windows version

No response

Additional context

No response

@JaiganeshKumaran JaiganeshKumaran added the bug Something isn't working label Aug 26, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Aug 26, 2022
@sylveon
Copy link
Contributor

sylveon commented Aug 26, 2022

Subfolders are supposed to represent sub-namespaces. Change the sub-namespace of the XAML control to match the subfolder.

@JaiganeshKumaran
Copy link
Contributor Author

@sylveon But then, there's this issue as well: #7600.

@bkudiess bkudiess added team-Markup Issue for the Markup team product-winui3 WinUI 3 issues needs-review 👀 labels Aug 26, 2022
@JaiganeshKumaran
Copy link
Contributor Author

Looks like it's still a problem even if you change the namespace. In that case, C++/WinRT prefixes the name of the header file with the subnamespace and then a dot (eg: Views.SettingsDialog.xaml.g.h). I would expect XAML to use the same name when the option to use subfolder for subnamespace files is not enabled.

@JaiganeshKumaran
Copy link
Contributor Author

XAML uses the same directory structure in C#. It'll be probably easier to just update C++/WinRT to include the correct header.

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Nov 27, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 28, 2022
@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@JaiganeshKumaran
Copy link
Contributor Author

Please fix this issue soon. It gets quickly messy with having all XAML files in the root folder.

@jamespack
Copy link

If you move the generated files out of the subfolder and into the root it works as a workaround. Not ideal, but we already have to go into that folder and move/copy things around.

@jamespack
Copy link

This likely should be filed over at cpp/winrt. Seems its an issue with that compiler.

@JaiganeshKumaran
Copy link
Contributor Author

JaiganeshKumaran commented May 5, 2023

Please look into this, really makes organising a codebase using WinUI in C++ harder.
@bkudiess
@StephenLPeters
@ryandemopoulos

@jaimecbernardo
Copy link

I've hit this issue when I wanted to basically have lots of applications sharing the same output directory. the .xaml files in the same directory meant that .xbf files would go into the wrong .pri files depending on racing conditions in the build process. So I wanted each C++ application to define a folder named like the project for its XAML files.

The way I managed to have it build is to add "$(ProjectDir)\Generated Files\IndividualXAMLDirectoryNameForThisProject" to the <AdditionalIncludedDirectories> of the vcxproj file. That means that all those xaml.g.h files can be resolved at build time now. Ugly, but gets the work done.

@michelgerritse
Copy link

michelgerritse commented Aug 30, 2024

I've hit the issue as well, and seem definitely related to how the XAML compiler outputs the generated files.
Is there any workaround available? I want my views, dialogs and other controls in their own sub namespace, located in their own project subfolder to have a nicely structured project.

Edit: OK, I've got a partial workaround:

Assume your project folder structure looks like this:

[MyProject]
[MyProject][Assets]
[MyProject][Views]
etc..

You can place your all your pages in the [MyProject][Views] folder and use the winrt::MyProject::Views to have a sub namespace for your views. However, you must prefix all your files with "Views.".

So for example, the [MyProject][Views] folder will contain:
Views.SettingsPage.xaml
Views.SettingsPage.idl
Views.SettingsPage.cpp
Views.SettingsPage.h

This still looks a bit ugly, but at least you don't have all code files in the root of your project folder, and you can have sub namespaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature proposal New feature proposal needs-review 👀 product-winui3 WinUI 3 issues team-Markup Issue for the Markup team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants