The first 10 Minutes of a C# Developer Exploring a Metro Application using Visual C++
Introduction
I’m a C# guy through and through but I have recently been exploring Visual C++ . I decided to take you with me as I spend 10 minutes exploring the Visual C++ Metro Templates included with Visual Studio 11. This blog post is written with my background in C#. I will try to point out the differences as I see them. We will look at Visual C++ templates solution explorer some XAML and even some code-behind.
Let’s get started with templates.
The Visual C++/C# Metro Templates built-into VS 11
Visual Studio 11 has the following templates built-into it for Visual C++/C# projects:
The description of each project type is located below:
- Application – A blank project using the Metro style framework.
- Grid Application – A multi-page project for navigating multiple layers of content with item details displayed on a dedicated page.
- Split Application – A project for navigating among a master list of items while viewing their details on the same page.
- Unit Test Project – A Unit Test Project.
- WinRT Component DLL – Template for a Windows Metro Style server DLL.
- DirectX Application – (Only in C++) Template for a blank Windows Metro style application using DirectX.
The only thing to note here is the inclusion of a DirectX Application with the C++ Templates. Everything else is pretty much identical in terms of the template structure.
The Solution Explorer
If you take a look at the “Application Template” for C++ vs. C# then you will notice some differences.
Looking at the C++ Solution Explorer in depth
- If you expand “External Dependencies” then you will see a long list of header files along with other files needed to build a C++ Metro application. This is necessary for the application to compile as we also have standard references needed to use certain libraries in a C# application.
- The Resources Folder contains 4 PNG images that we are used to seeing inside of the Images folder in a C# Application.
- The only difference in the App.xaml and MainPage.xaml is that in the C++ application it contains both a .cpp (C ++) and .h (header) file whereas the C# version only contains the .cs (C Sharp) file.
- The Package.appxmanifest exist in both projects and contains metadata that describes your app including display name description logos and capabilities.
- Finally we get to the pch.cpp/pch.h files which are pre-compiled header files and are obviously not included in the C# application.
The other thing you might notice is that the C# application contains an AssemblyInfo.cs under Properties whereas the C++ version did not. This file consists of all of the build options for the project including version company name GUID compilers options etc.
Looking at a simple “Hello World” Application – The XAML
A button and a TextBlock… it doesn’t get any easier than this. If the user clicks the button then it displays “Hello World” in a TextBlock as shown below.
Thankfully XAML is XAML – the following code snippet works regardless if you are using C# VB or C++.
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C"> <StackPanel> <Button Content="Click Me" FontSize="24" Width="200" Height="60" Click="HelloButton_Click" /> <TextBlock x:Name="DisplayText" FontSize="48" Foreground="White" /> </StackPanel> </Grid>
The Code Behind
You knew this part would be massively different didn’t you? I hope so – we are talking about an entirely different language. :)
Below is a “Hello World” that works with the XAML included above in C#:
private void HelloButton_Click(object sender RoutedEventArgs e) { DisplayText.Text = "Hello World"; }
That same “Hello World” looks like the following in C++
void HelloWorld::MainPage::HelloButton_Click(Platform::Object^ sender Windows::UI::Xaml::RoutedEventArgs^ e) { DisplayText->Text = "Hello World"; }
Look at the way the event handler is setup. Looks foreign right now doesn’t it? No worries give this free e-Book a spin to convert your existing C# skills to C++.
Wrap-up
What did we learn in 10 minutes? We learned about several differences in C# and Visual C++ Metro Applications. But the real challenge here isn’t learning the new templates differences in solution explorer or XAML. It is that we need to learn the language. I’m not going to go into why I’m spending nights/weekends dabbling in Visual C++ bu
Leave a Comment