Learning by example is a great way to get up to speed on new coding techniques. Microsoft has delivered a ton of examples available for download to help you see concrete illustrations of specific features of the new Metro style apps. You can download single files for specific languages such as C++, C#, JavaScript and VB.NET or download one file with everything.
It will soon become apparent as you look through the different samples that XAML is the common way to build a Metro user interface (UI). If you’ve been avoiding learning XAML up to this point, you might want to reconsider that decision. While you will still be able to build apps based on the old Windows Forms and run them in compatibility mode, all Metro apps must use XAML.
Installing
To try out the samples you’ll need to have a machine with the Windows 8 Consumer Preview installed and the Visual Studio 11 beta. Before you unpack the download file you’ll want to take into consideration the requirement to save the files on an NTFS volume if you want to run the code from that location. With the files unpacked you should be able to double-click on the .sln file and open it in Visual Studio.
This is probably a good place to point out the fact that you get a copy of Blend for Visual Studio when you install the Visual Studio 11 beta. Blend is integrated even more tightly with Visual Studio 11 than in previous releases. You can edit XAML code in Blend and have the results show up in Visual Studio with a simple refresh.
Specific Samples
Dealing with files is a common task most programmers must deal with at some point. The samples kit includes a number of examples of dealing with files in the Metro UI. If you’re a VB.NET fan you’ll find several file samples including “Enumerate files and folders in a location”, “File access sample”, and “Query files in a location with Advanced Query Syntax”. The last example demonstrates a number of new API calls including the QueryOptions and StorageFileQueryResult classes along with various methods from these classes.
To build and run this sample either load it into Visual Studio using the File / Open / Project/Solution method or double-click on the FileSearchSample.sln file. You should see a screen like the one in Figure 1. The C# camp will find quite a few more samples, a total of 115 versus 44 for VB.NET.
Figure 1
Many of the sample apps are coded in all four languages. This makes it really easy to compare the different coding methods if you’re comfortable with one language and looking to expand to others. It’s also a good way to get an idea of the amount of code required to solve specific problems in the different languages. Here’s a quick comparison of the “File access sample” in terms of numbers of files:
Language |
FileName |
Size |
Lines |
# Files |
C++ |
MainPage.xaml.cpp |
25010 |
533 |
102 |
C# |
MainPage.xaml.cs |
27751 |
555 |
76 |
VB.NET |
MainPage.xaml.vb |
24169 |
429 |
32 |
JavaScript |
Program.js |
17374 |
320 |
25 |
Figure 2 shows what the running program looks like while Figures 3 – 6 show the code for each language open in the IDE. You can get an idea of the project contents by looking in the solution explorer window on the right side of the screen shot. The running program is essentially the same for all four languages.
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
For the “Query files in a location with Advanced Query Syntax” sample the numbers are similar:
Language |
FileName |
Size |
Lines |
# Files |
C++ |
MainPage.xaml.cpp |
4486 |
111 |
28 |
C# |
MainPage.xaml.cs |
5955 |
132 |
25 |
VB.NET |
MainPage.xaml.vb |
5368 |
98 |
25 |
JavaScript |
Program.js |
2446 |
47 |
23 |
Here are the results from the “Accelerometer Sensor Sample”:
Language |
FileName |
Size |
Lines |
# Files |
C++ |
MainPage.xaml.cpp |
9765 |
244 |
28 |
C# |
MainPage.xaml.cs |
11098 |
243 |
24 |
VB.NET |
MainPage.xaml.vb |
9888 |
179 |
25 |
JavaScript |
Program.js |
5741 |
125 |
22 |
While this isn’t a statistically significant conclusion nor does it look at a full spectrum of samples, it does seem to show JavaScript as the language with the least amount of coding required. You could also put up an argument about the amount of supporting code required for all the languages, but in the final analysis it really comes down to how much a programmer has to type to implement a solution.
Final Thoughts
Microsoft has provided a great resource that should appeal to programmers with varied language preferences. It’s also a good way to see how specific coding patterns are implemented in the different languages. You’ll definitely want to download the files if you’re looking to get a head start on Windows 8 Metro application development.