Skip to content

Commit

Permalink
Various fixes for C++ project and item templates (#3745)
Browse files Browse the repository at this point in the history
This is a partial adoption of #3619 by @JaiganeshKumaran in order to expedite the merging of the changes from that PR which do not need additional time for discussion. The individual changes that were extracted from the aforementioned PR are as follows:

- Removed usage of InitializeComponent as it's no longer recommended by C++/WinRT (see https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent for more details).
- Removed Microsoft copyright notice.
- Removed #include XAML headers in a template control header. This was in the C++/WinRT templates because a runtime component will not include XAML headers in pch.h, which is no longer the case with a WinUI 3 runtime component.
- Made the WinUI 3 runtime component "desktop-only" as WinUI 3 cannot be used by UWP apps. This is a fix for #1780. (Note that unlike the original PR the source directory structure is not being changed in order to minimize potential merge conflicts.)
- Removed App.idl because it's empty and not needed.
- Indented App.xaml.cpp's content inside the implementation namespace, rather than `using namespace` for consistency with the rest of the code. Also removed `using namespace` for namespaces that aren't actually used (Controls and Navigation namespaces were needed for Frame navigation, which isn't performed in the WinUI 3 template. IInspectable is available as an alias in every WinRT implementation type, thus doesn't require prefixing it with Windows::Foundation).

Additionally, this PR replaces `<ProjectTypeTag>uwp</ProjectTypeTag>` with `<ProjectTypeTag>WinUI</ProjectTypeTag>` (or just adds the latter if the former was not present at all) in the item templates. I don't believe this tag is surfaced anywhere in the Visual Studio UI but the consistency makes me happier.

---------

Co-authored-by: Jaiganésh Kumaran <[email protected]>
  • Loading branch information
evelynwu-msft and JaiganeshKumaran authored Jul 20, 2023
1 parent e1ae250 commit 18ada2f
Show file tree
Hide file tree
Showing 61 changed files with 119 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<LanguageTag>csharp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem OpenInEditor="true" ReplaceParameters="true" ItemType="Page" CustomTool="MSBuild:Compile" TargetFileName="$fileinputname$.xaml">BlankWindow.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.xaml.h"
#if __has_include("$safeitemname$.g.cpp")
Expand All @@ -15,11 +12,6 @@ using namespace Microsoft::UI::Xaml;

namespace winrt::$rootnamespace$::implementation
{
$safeitemname$::$safeitemname$()
{
InitializeComponent();
}

int32_t $safeitemname$::MyProperty()
{
throw hresult_not_implemented();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once

#include "$safeitemname$.g.h"
Expand All @@ -9,12 +6,16 @@ namespace winrt::$rootnamespace$::implementation
{
struct $safeitemname$ : $safeitemname$T<$safeitemname$>
{
$safeitemname$();
$safeitemname$()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}

int32_t MyProperty();
void MyProperty(int32_t value);

void myButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
void myButton_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

namespace $rootnamespace$
{
[default_interface]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<LanguageTag>cpp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem SubType="Designer" TargetFileName="$fileinputname$.xaml" ReplaceParameters="true">BlankWindow.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>csharp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem OpenInEditor="true" ReplaceParameters="true" ItemType="Page" CustomTool="MSBuild:Compile" TargetFileName="$fileinputname$.xaml">BlankPage.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>csharp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem OpenInEditor="true" ReplaceParameters="true" ItemType="Page" CustomTool="MSBuild:Compile" TargetFileName="$fileinputname$.xaml">ResourceDictionary.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>csharp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem ItemType="PRIResource">Resources.resw</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>csharp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$.cs">CustomControl.cs</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>csharp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem OpenInEditor="true" ReplaceParameters="true" ItemType="Page" CustomTool="MSBuild:Compile" TargetFileName="$fileinputname$.xaml">UserControl.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.xaml.h"
#if __has_include("$safeitemname$.g.cpp")
Expand All @@ -15,11 +12,6 @@ using namespace Microsoft::UI::Xaml;

namespace winrt::$rootnamespace$::implementation
{
$safeitemname$::$safeitemname$()
{
InitializeComponent();
}

int32_t $safeitemname$::MyProperty()
{
throw hresult_not_implemented();
Expand Down
11 changes: 6 additions & 5 deletions dev/VSIX/ItemTemplates/Neutral/CppWinRT/BlankPage/BlankPage.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once

#include "$safeitemname$.g.h"
Expand All @@ -9,12 +6,16 @@ namespace winrt::$rootnamespace$::implementation
{
struct $safeitemname$ : $safeitemname$T<$safeitemname$>
{
$safeitemname$();
$safeitemname$()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}

int32_t MyProperty();
void MyProperty(int32_t value);

void myButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
void myButton_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

namespace $rootnamespace$
{
[default_interface]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>cpp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem SubType="Designer" TargetFileName="$fileinputname$.xaml" ReplaceParameters="true">BlankPage.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>cpp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem OpenInEditor="true" ReplaceParameters="true" ItemType="Page" TargetFileName="$fileinputname$.xaml">ResourceDictionary.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>cpp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem ItemType="PRIResource">Resources.resw</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.h"
#if __has_include("$safeitemname$.g.cpp")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once

#include "winrt/Microsoft.UI.Xaml.h"
#include "winrt/Microsoft.UI.Xaml.Markup.h"
#include "winrt/Microsoft.UI.Xaml.Controls.Primitives.h"
#include "$safeitemname$.g.h"

namespace winrt::$rootnamespace$::implementation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

namespace $rootnamespace$
{
[default_interface]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>cpp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem SubType="Code" TargetFileName="$fileinputname$.cpp" ReplaceParameters="true">TemplatedControl.cpp</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.xaml.h"
#if __has_include("$safeitemname$.g.cpp")
Expand All @@ -15,11 +12,6 @@ using namespace Microsoft::UI::Xaml;

namespace winrt::$rootnamespace$::implementation
{
$safeitemname$::$safeitemname$()
{
InitializeComponent();
}

int32_t $safeitemname$::MyProperty()
{
throw hresult_not_implemented();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once

#include "winrt/Microsoft.UI.Xaml.h"
#include "winrt/Microsoft.UI.Xaml.Markup.h"
#include "winrt/Microsoft.UI.Xaml.Controls.Primitives.h"
#include "$safeitemname$.g.h"

namespace winrt::$rootnamespace$::implementation
{
struct $safeitemname$ : $safeitemname$T<$safeitemname$>
{
$safeitemname$();
$safeitemname$()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}

int32_t MyProperty();
void MyProperty(int32_t value);

void myButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
void myButton_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

namespace $rootnamespace$
{
[default_interface]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<TargetPlatformName>Windows</TargetPlatformName>
<LanguageTag>cpp</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>uwp</ProjectTypeTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<ProjectItem SubType="Designer" TargetFileName="$fileinputname$.xaml" ReplaceParameters="true">UserControl.xaml</ProjectItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<LanguageTag>XAML</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>WinUI</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent PreferedSolutionConfiguration="Debug|AnyCPU">
<CustomParameters>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<LanguageTag>XAML</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>WinUI</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent PreferedSolutionConfiguration="Debug|x86">
<Project File="ProjectTemplate.csproj" ReplaceParameters="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<LanguageTag>XAML</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>WinUI</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent>
<CustomParameters>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<LanguageTag>XAML</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>WinUI</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent PreferedSolutionConfiguration="Debug|x86">
<CustomParameters>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<LanguageTag>XAML</LanguageTag>
<PlatformTag>windows</PlatformTag>
<ProjectTypeTag>desktop</ProjectTypeTag>
<ProjectTypeTag>WinUI</ProjectTypeTag>
<ProjectTypeTag>winui</ProjectTypeTag>
</TemplateData>
<TemplateContent PreferedSolutionConfiguration="Debug|x86">
<CustomParameters>
Expand Down
Loading

0 comments on commit 18ada2f

Please sign in to comment.