The first 10 Minutes of a C# Developer Exploring a Metro Application using Visual C++

image

 

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:

image

image

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.

imageimage

Looking at the C++ Solution Explorer in depth

  1. 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.
  2. The Resources Folder contains 4 PNG images that we are used to seeing inside of the Images folder in a C# Application.
  3. 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.
  4. The Package.appxmanifest exist in both projects and contains metadata that describes your app, including display name, description, logos, and capabilities.
  5. 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.

image

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++, but I think learning native application development is a great way to go. I’ll probably expand on this post with a more in-depth article on building a complete application with Visual C++ if enough people are interested. Until then - take care everyone!



Windows 8 WinRT
Posted by: Michael Crump
Last revised: 05 Mar, 2012 10:21 PM

Comments

John Blackburn
John Blackburn
17 Feb, 2012 11:22 AM

A couple of points:-

1.) What you have written isn't really pure C++ it is C++/CLI

2.) The lengthy namespace definitions should be shorter by means of "using" statements to make it more comparable on an even level.

Paul
Paul
17 Feb, 2012 12:16 PM

Perhaps a good starting point, but that isn't native C++. It gets compiled to the same bytecode that C# is compiled to.

Arild Fiskum
Arild Fiskum
17 Feb, 2012 01:03 PM

No, this is native C++. They reused the ^ from C++/Cli as a compiler extension, but its native compiled C++.

Discussion

Mark
Mark
17 Feb, 2012 01:43 PM

This is far from the standard C++ syntax. There is no point developing with such messed up language like C++/CLI.

Jean-Michel Deruty
Jean-Michel Deruty
17 Feb, 2012 02:46 PM

Like Arild, I must insist:

This is NOT C++/CLI. C++/CLI was C++ compiled into il and run with the CLR. It's therefore a .NET language.

the C++ available for WinRT is native, but provides the ^ operator as a compiler extension. I don't know the exact meaning of ^ however. (I knew it for C++/CLI: "garbage collected reference", but here? It looks like "pointers to WinRT instances".)

The important thing being that if you build a C++ metro app, it won't use the .NET CLR. If it was the case: 1) On the WinRT architecture slide, C++ would be under .NET with C#. 2) DirectX wouldn't be only available to C++.

17 Feb, 2012 06:02 PM

Like others have said, this is not CLI/C++, even though the syntax is very similar. CLI/C++ would imply that the CLR or byte code is involved. C++ /CX generates C++ code for the compiler, which makes authoring and consuming WinRT components very easy to do.

GaryM
GaryM
18 Feb, 2012 03:13 PM

It is also NOT C++.

Jean-michel Deruty
Jean-michel Deruty
18 Feb, 2012 07:17 PM

It is C++: the standard allows compiler extensions like the one implemented by ms: ^ is only syntactic sugar.

Charlie
Charlie
20 Feb, 2012 04:37 PM

I've done C++ for years, but also C#. Note C++ is multiple O/S, Microsoft extensions like ^ are not in Unix compilers.

Last I knew, ^ was the .net equivalent of C++'s * for pointers, since .net memory is different than standard C++'s memory.

I'm not familiar with Metro; it doesn't appear available in Visual Studio 2010. C++ programmers don't tend to use the latest software, an introduction might be helpful, in case that would explain why we think ^ implies .net, and it somehow doesn't.

Simon
Simon
20 Feb, 2012 10:14 PM

@Charlie. Where have you been hiding? WinRT is the new runtime being shipped with Windows 8 and future Windows based tablet devices. Metro is the design paradigm for WinRT and also the Silverlight based Windows Phone 7.

For more info on WinRT check out http://www.wintellect.com/CS/blogs/jlikness/archive/2012/02/09/windows-8-the-facts-about-arm-metro-and-the-blue-stack.aspx

And just to set everyone straight, this IS C++ with compiler extensions, and teh ^ is a pointer to a managed object on the garbage collected heap as Jean pointed out.

23 Feb, 2012 12:52 PM

Here's a nice introduction into the WinRT C++ extensions; http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/1996

It's interesting yet slightly depressing what things aren't in the ISO standard yet (like properties, events, delegates, binary compatibility), but in any case the extensions should only be used at the UI/WinRT level.

No new comments are allowed on this post.

Hosting provided by http://www.DiscountASP.NET