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

Improved C++ item & templates #3619

Open
wants to merge 10 commits into
base: main-old
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project ToolsVersion="Current">
<Project ToolsVersion="Current">
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
Expand Down Expand Up @@ -129,9 +129,9 @@
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<IncludeOutputGroupsInVSIX>TemplateProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
</ProjectReference>
<ProjectReference Include="$(ProjectTemplatesDir)Neutral\CppWinRT\RuntimeComponent\WinUI.Neutral.CppWinRT.RuntimeComponent.csproj">
<ProjectReference Include="(ProjectTemplatesDir)Desktop\CppWinRT\RuntimeComponent\WinUI.Desktop.CppWinRT.RuntimeComponent.csproj">
<Project>{FD33E535-127D-48AC-8788-0F61FA2A251C}</Project>
<Name>WinUI.Neutral.CppWinRT.RuntimeComponent</Name>
<Name>WinUI.Desktop.CppWinRT.RuntimeComponent</Name>
<VSIXSubPath>ProjectTemplates</VSIXSubPath>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<IncludeOutputGroupsInVSIX>TemplateProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project ToolsVersion="Current">
<Project ToolsVersion="Current">
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
Expand Down Expand Up @@ -132,9 +132,9 @@
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<IncludeOutputGroupsInVSIX>TemplateProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
</ProjectReference>
<ProjectReference Include="$(ProjectTemplatesDir)Neutral\CppWinRT\RuntimeComponent\WinUI.Neutral.CppWinRT.RuntimeComponent.csproj">
<ProjectReference Include="(ProjectTemplatesDir)Desktop\CppWinRT\RuntimeComponent\WinUI.Desktop.CppWinRT.RuntimeComponent.csproj">
<Project>{FD33E535-127D-48AC-8788-0F61FA2A251C}</Project>
<Name>WinUI.Neutral.CppWinRT.RuntimeComponent</Name>
<Name>WinUI.Desktop.CppWinRT.RuntimeComponent</Name>
<VSIXSubPath>ProjectTemplates</VSIXSubPath>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<IncludeOutputGroupsInVSIX>TemplateProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.xaml.h"
#if __has_include("$safeitemname$.g.cpp")
#include "$safeitemname$.g.cpp"
#endif

using namespace winrt;
using namespace Microsoft::UI::Xaml;
Expand All @@ -15,23 +10,4 @@ using namespace Microsoft::UI::Xaml;

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

int32_t $safeitemname$::MyProperty()
{
throw hresult_not_implemented();
}

void $safeitemname$::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}

void $safeitemname$::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
}
}
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,11 @@ namespace winrt::$rootnamespace$::implementation
{
struct $safeitemname$ : $safeitemname$T<$safeitemname$>
{
$safeitemname$();

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

void myButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
$safeitemname$()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}
};
}

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

namespace $rootnamespace$
{
[default_interface]
runtimeclass $safeitemname$ : Microsoft.UI.Xaml.Window
{
$safeitemname$();
Int32 MyProperty;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<Window
x:Class="$rootnamespace$.$safeitemname$"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:$rootnamespace$"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:$rootnamespace$"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
<Grid>

</Grid>
</Window>
24 changes: 0 additions & 24 deletions dev/VSIX/ItemTemplates/Neutral/CppWinRT/BlankPage/BlankPage.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.xaml.h"
#if __has_include("$safeitemname$.g.cpp")
#include "$safeitemname$.g.cpp"
#endif

using namespace winrt;
using namespace Microsoft::UI::Xaml;
Expand All @@ -15,23 +10,4 @@ using namespace Microsoft::UI::Xaml;

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

int32_t $safeitemname$::MyProperty()
{
throw hresult_not_implemented();
}

void $safeitemname$::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}

void $safeitemname$::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
}
}
14 changes: 5 additions & 9 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,11 @@ namespace winrt::$rootnamespace$::implementation
{
struct $safeitemname$ : $safeitemname$T<$safeitemname$>
{
$safeitemname$();

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

void myButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
$safeitemname$()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}
};
}

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

namespace $rootnamespace$
{
[default_interface]
runtimeclass $safeitemname$ : Microsoft.UI.Xaml.Controls.Page
{
$safeitemname$();
Int32 MyProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
<Grid>

</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "$safeitemname$.h"
#if __has_include("$safeitemname$.g.cpp")
#include "$safeitemname$.g.cpp"
#endif

using namespace winrt;
using namespace Microsoft::UI::Xaml;
Expand All @@ -17,16 +12,6 @@ namespace winrt::$rootnamespace$::implementation
{
$safeitemname$::$safeitemname$()
{
DefaultStyleKey(winrt::box_value(L"$rootnamespace$.$safeitemname$"));
}

int32_t $safeitemname$::MyProperty()
{
throw hresult_not_implemented();
}

void $safeitemname$::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
DefaultStyleKey(winrt::box_value(xaml_typename<class_type>()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xaml_typename<class_type>()

XAML accepts boxed strings so this change isn't necessary. Can you provide a min repro project for the crash you were encountering with the previous incarnation?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a crash, it's a compile error. C++/WinRT hstring does not implicitly convert to IInspectable. It needs to be explicitly done (via winrt::box_value). The template won't compile by default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. However, the change being proposed here is simply from hstring to xaml_typename; the enclosing winrt::box_value() call isn't being touched.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. xaml_typename is a C++/WinRT helper that gives a Windows::UI::Xaml::Interop::TypeName for the type. This matches more closely to the C# template (which uses typeof instead of a string), but the runtime behavior is the same (with both the new code and the old code, the runtime will look for <Style TargetType="$rootnamespace$.$safeitemname$">)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary benefit of using xaml_typename<class_type>() over 'hstring' that I'm aware of is that it simplifies renaming the class. But the argument being made here instead is that there's an error associated with the hstring variant so that's why I want to dig deeper.

Copy link
Contributor Author

@JaiganeshKumaran JaiganeshKumaran Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right DefaultStyleKey can be set to a boxed hstring as well, it doesn't have to be a TypeName. As an alternative, winrt::name_of could be used instead of winrt::xaml_typename. There are a few differences between the name_of and xaml_typename, for example, name_of<Windows::Foundation::Point>() returns "Windows.Foundation.Point" while xaml_typename<Windows::Foundation::Point>().Name is "Point". This doesn't matter in this case though.

}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
// 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$();

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

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

namespace $rootnamespace$
{
[default_interface]
runtimeclass $safeitemname$ : Microsoft.UI.Xaml.Controls.Control
{
$safeitemname$();
Int32 MyProperty;
}
}
17 changes: 5 additions & 12 deletions dev/VSIX/ItemTemplates/Neutral/CppWinRT/UserControl/UserControl.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
// 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$();

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

void myButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
$safeitemname$()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}
};
}

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

namespace $rootnamespace$
{
[default_interface]
runtimeclass $safeitemname$ : Microsoft.UI.Xaml.Controls.UserControl
{
$safeitemname$();
Int32 MyProperty;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<UserControl
x:Class="$rootnamespace$.$safeitemname$"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:$rootnamespace$"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:$rootnamespace$"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
<Grid>

</Grid>
</UserControl>
Loading