Building Your First Windows Store App

Since the launch of Windows 8, Windows Store apps are the new way to build applications for Windows and Windows Phone. C# Corner has over 400 articles on Windows Store apps.

Learn Windows Store App development >>

For the last few months, I've been working on a C# Corner Windows Store app and side by side writing a book and these articles.

In this article, you will learn how to write your first “Hello World!” application for Windows Store. You will need Visual Studio 2013 or Visual Studio 2014 to follow this tutorial. I used Visual Studio 2014.

Let's create a simple “Hello, Windows!” app.

Create a Project in Visual Studio

Open Visual Studio and select the "File" >> "New" >> "Project..." command.

In New Project, you will see Installed Templates in the left side templates listing. In the left side, you have a choice to select a language, either Visual Basic, Visual C# or Visual C++.

I selected "Visual C#" -> "Store App" > "Windows App".

In the centre area, make sure you have .NET Framework 4.5.1 selected in the first drop down list and the Blank App (Windows) is selected as the project type template. This template is used to create a Blank Windows Store app. As you can see from the right most side, a blank project is used to create a single-page Windows app that has no predefined controls or layout. See Figure 1.

Visual Studio Store App Project Templates

Figure 1 Visual Studio Store App Project Templates

Now on the Name text box, type the name of your project. For my project, I named it HelloWindows. The Location text blow lets you browse to the folder where you would like to store your project files. You can even change the solution name if you would like by changing the Solution Name field. This field is important, when you have multiple projects in a single solution. I keep my Solution Name as the default.

Note: As you can see in Figure 1, there are three types of Windows Store Apps, Universal Apps, Windows Apps and Windows Phone Apps. Windows Apps deploy and run on Windows 8 (or later versions) PCs and tablets only. Windows Phone Apps run on Windows Phone devices only. The Universal Apps run on both Windows PCs and Windows Phone devices. Right now, the Universal Apps have limited functionality.

Now click the OK button.

Understanding Project and Files

Once you have clicked on the OK button, you will see the default Visual Studio solution that looks like Figure 2.

Visual Studio 2014 IDE

Figure 2 Visual Studio 2014 IDE

As you can see from Figure 2, there are 3 major areas of Visual Studio; the Code Editor, Solution Explorer and Properties window. This may look different based on your previous Visual Studio settings.

First I want to focus on the Solution Explorer. Figure 3 is the expanded view of the Solution Explorer.

Solution Explorer

Figure 3 Solution Explorer

The Solution Explorer is a TreeView like control that lists all projects and project files of a Solution. By default you will see the four nodes, Analyzers, Properties, References, Assets, App.xaml, a TemporaryKey, MainWindow.xaml and Package.appxmanifest.

The Properties node has a file called AssemblyInfo.cs that stores the general information about an assembly. In this case, the current app.

The References folder lists all the references added to a project.

The Assets folder list all the assets available to this app. As you can see from Figure 4, there are four .png files added by default to the project that are used as default logos for the app.

The App.xaml file is application file and stores application related code.

MainWindow.xaml and the class is the representation of the main user interface window of the app.

Package.appxmanifest files define the app package manifest.

Assembly Information

AssemblyInfo.cs file stores all assembly information. If you double-click on the AssemblyInfo.cs in the Solution Explorer, you will see Listing 1. The AssemblyInfo file defines the app attributes such as the title, description, company name, copyright and version. I know many developers ignore this but in today's world, you must specify all the information including your company name, copyright and trademark, if any.

  1. using System.Reflection;  
  2. using System.Runtime.CompilerServices;  
  3. using System.Runtime.InteropServices;  
  4.   
  5. // General Information about an assembly is controlled through the following   
  6. // set of attributes. Change these attribute values to modify the information  
  7. // associated with an assembly.  
  8. [assembly: AssemblyTitle("HelloWindows")]  
  9. [assembly: AssemblyDescription("")]  
  10. [assembly: AssemblyConfiguration("")]  
  11. [assembly: AssemblyCompany("")]  
  12. [assembly: AssemblyProduct("HelloWindows")]  
  13. [assembly: AssemblyCopyright("Copyright ©  2014")]  
  14. [assembly: AssemblyTrademark("")]  
  15. [assembly: AssemblyCulture("")]  
  16.   
  17. // Version information for an assembly consists of the following four values:  
  18. //  
  19. //      Major Version  
  20. //      Minor Version   
  21. //      Build Number  
  22. //      Revision  
  23. //  
  24. // You can specify all the values or you can default the Build and Revision Numbers   
  25. // by using the '*' as shown below:  
  26. // [assembly: AssemblyVersion("1.0.*")]  
  27. [assembly: AssemblyVersion("1.0.0.0")]  
  28. [assembly: AssemblyFileVersion("1.0.0.0")]  
  29. [assembly: ComVisible(false)] 

Listing 1

The version is probably the most important part of this file. It allows you to define assembly and file versions that is a combination of Major Version, Minor Version, Build Number and Revision. As you can see from the Listing 1, by setting the value “1.0.*” it will automatically increment the build number every time you rebuild the app.

Application Class

The application definitions and events are declared in the App.xaml and its code-behind App.xaml.cs files. App.xaml file declares application level resources and App.xaml.cs defines the application level events. See Listing 2.

  1. <Application  
  2.     x:Class="HelloWindows.App"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:HelloWindows">  
  6.   
  7. </Application> 

Listing 2 App.xaml

Listing 3 shows the App.xaml.cs file.

  1. namespace HelloWindows  
  2. {  
  3.     /// <summary>  
  4.     /// Provides application-specific behavior to supplement the default Application class.  
  5.     /// </summary>  
  6.     sealed partial class App : Application  
  7.     {  
  8.         /// <summary>  
  9.         /// Initializes the singleton application object.  This is the first line of authored code  
  10.         /// executed, and as such is the logical equivalent of main() or WinMain().  
  11.         /// </summary>  
  12.         public App()  
  13.         {  
  14.             this.InitializeComponent();  
  15.             this.Suspending += OnSuspending;  
  16.         }  
  17.   
  18.         /// <summary>  
  19.         /// Invoked when the application is launched normally by the end user.  Other entry points  
  20.         /// will be used such as when the application is launched to open a specific file.  
  21.         /// </summary>  
  22.         /// <param name="e">Details about the launch request and process.</param>  
  23.         protected override void OnLaunched(LaunchActivatedEventArgs e)  
  24.         {  
  25.  
  26. #if DEBUG  
  27.             if (System.Diagnostics.Debugger.IsAttached)  
  28.             {  
  29.                 this.DebugSettings.EnableFrameRateCounter = true;  
  30.             }  
  31. #endif  
  32.   
  33.             Frame rootFrame = Window.Current.Content as Frame;  
  34.   
  35.             // Do not repeat app initialization when the Window already has content,  
  36.             // just ensure that the window is active  
  37.             if (rootFrame == null)  
  38.             {  
  39.                 // Create a Frame to act as the navigation context and navigate to the first page  
  40.                 rootFrame = new Frame();  
  41.                 // Set the default language  
  42.                 rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];  
  43.   
  44.                 rootFrame.NavigationFailed += OnNavigationFailed;  
  45.   
  46.                 if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)  
  47.                 {  
  48.                     //TODO: Load state from previously suspended application  
  49.                 }  
  50.   
  51.                 // Place the frame in the current Window  
  52.                 Window.Current.Content = rootFrame;  
  53.             }  
  54.   
  55.             if (rootFrame.Content == null)  
  56.             {  
  57.                 // When the navigation stack isn't restored navigate to the first page,  
  58.                 // configuring the new page by passing required information as a navigation  
  59.                 // parameter  
  60.                 rootFrame.Navigate(typeof(MainPage), e.Arguments);  
  61.             }  
  62.             // Ensure the current window is active  
  63.             Window.Current.Activate();  
  64.         }  
  65.   
  66.         /// <summary>  
  67.         /// Invoked when Navigation to a certain page fails  
  68.         /// </summary>  
  69.         /// <param name="sender">The Frame which failed navigation</param>  
  70.         /// <param name="e">Details about the navigation failure</param>  
  71.         void OnNavigationFailed(object sender, NavigationFailedEventArgs e)  
  72.         {  
  73.             throw new Exception("Failed to load Page " + e.SourcePageType.FullName);  
  74.         }  
  75.   
  76.         /// <summary>  
  77.         /// Invoked when application execution is being suspended.  Application state is saved  
  78.         /// without knowing whether the application will be terminated or resumed with the contents  
  79.         /// of memory still intact.  
  80.         /// </summary>  
  81.         /// <param name="sender">The source of the suspend request.</param>  
  82.         /// <param name="e">Details about the suspend request.</param>  
  83.         private void OnSuspending(object sender, SuspendingEventArgs e)  
  84.         {  
  85.             var deferral = e.SuspendingOperation.GetDeferral();  
  86.             //TODO: Save application state and stop any background activity  
  87.             deferral.Complete();  
  88.         }  
  89.     }  

Listing 3 App.xaml.cs

The code-behind file, App.xaml.cs, has the three methods definitions, OnLaunched, OnNavigationFailed and OnSuspending. The OnLaunched method is invoked when the application is launched. The OnNavigationFailed method is invoked when Navigation to a certain page fails. The OnSuspending method is invoked when application execution is being suspended.

The Main Page

Every Windows Store app consists of one default MainPage.xaml file. This is the default user interface the app users will see when the app is launched. The MainPage.xaml is the main user interface of an app. This is the screen users see then they launch their app.

Double-click on the MainPage.xaml file in Solution Explorer. The page opens in the designer with the XAML view. See Figure 4.

Main Page

Figure 4 Main Page

As you can see from Figure 4 XAML, the MainPage is derived from the Page class. The XAML also shows there is a Grid element added to the interface automatically that works as a container for the child controls.

Now to add controls to MainPage, simply drag and drop controls from Toolbox. I will drag and drop a Button, a TextBox and a TextBlock control from Toolbox to MainPage. After moving and placing controls around, my MainPage looks like Figure 5.

Page Layout

Figure 5 Page Layout

Now one thing you may have noticed when you were dragging and positioning these controls in MainPage. The designer was writing XAML code for you. After placing control same as Figure 5, the XAML code looks like Listing 4.

  1. <Page  
  2.     x:Class="HelloWindows.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:HelloWindows"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  11.         <Button Content="Button" HorizontalAlignment="Left" Margin="57,172,0,0"  
  12.                 VerticalAlignment="Top" Height="99" Width="277"/>  
  13.         <TextBox HorizontalAlignment="Left" Margin="384,175,0,0" TextWrapping="Wrap"  
  14.                  Text="TextBox" VerticalAlignment="Top" Height="91" Width="628"/>  
  15.         <TextBlock HorizontalAlignment="Left" Height="208" Margin="60,313,0,0"  
  16.                    TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"   
  17.                    Width="952"/>  
  18.   
  19.     </Grid>  
  20. </Page> 

Listing 4 MainPage.xaml

As you can see from Listing 4, there is one XAML element for each Button, TextBox and TextBlock controls. Each element also has Width, Height, Margin, VerticalAlignment and HorizontalAlignment properties set for these controls.

Setting Controls Properties

The next step is to set control properties. There are two ways to set the control properties. The first and the easiest option is the use the Properties window and the second, you can write XAML code by hand. For the sake of simplicity, we will use the designer for now.

To open the Properties window, select a control and click on the Properties tab in Solution Explorer. For example, select the Button control and then click on the Properties tab, the Properties window for the Button control looks like Figure 6.

Properties Window

Figure 6 Properties Window

Figure 6 shows the Properties window that lists all the properties of a control. You can simply set these properties by selecting the property and its value.

I select the Button control and change its Name to “HelloButton”, Brush background to Red and Content property to “Click Me!” as you can see from Figure 7. I also change the font size to 36.

Setting Control Properties

Figure 7 Setting Control Properties

If you look at the XAML code, the new XAML of Button looks like Listing 5 that shows the values of x:Name, Content, Background, FontSize and FontFamily.

  1. <Button x:Name="HelloButton" Content="Click Me!" HorizontalAlignment="Left"   
  2.                 Margin="57,172,0,0" VerticalAlignment="Top" Height="99" Width="277"                  Background="#FFD52626" FontSize="36" FontFamily="Global User Interface"/>  

Listing 5

I also set the x:Name, FontName, FontSize and other properties of the TextBox and the TextBlock control. You can proceed and change your control the way you like. The final XAML of the controls look like Listing 6.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.         <Button x:Name="HelloButton" Content="Click Me!" HorizontalAlignment="Left"   
  3.                 Margin="57,172,0,0" VerticalAlignment="Top" Height="99" Width="277"   
  4.                 Background="#FFD52626" FontSize="36" FontFamily="Global User Interface" Click="HelloButton_Click"/>  
  5.         <TextBox x:Name="HelloTextBox" HorizontalAlignment="Left" Margin="384,175,0,0" TextWrapping="Wrap"  
  6.                  Text="TextBox" VerticalAlignment="Top" Height="91" Width="628" FontSize="24"/>  
  7.         <TextBlock x:Name="HelloTextBlock" HorizontalAlignment="Left" Height="208" Margin="60,313,0,0"  
  8.                    TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"   
  9.                    Width="952" FontSize="48" Foreground="#FFE3C855"/> 

Listing 6

Adding Event Handler

To see all control events and add event handlers, click on the Lightening icon on the Properties window. It will open the events. See Figure 8.

Adding Event Handler
Figure 8



To add an event handler, simply double-click on the event TextBox. I double-click on the HelloBotton's Click event's TextBox. It adds the following code to the code-behind file. See Listing 7.

  1. private void HelloButton_Click(object sender, RoutedEventArgs e)  
  2. {} 

Listing 7

The Button control XAML is also updated and the Click attribute is added to it. See Listing 8.

  1. x:Name="HelloButton" Content="Click Me!" HorizontalAlignment="Left"   
  2.                 Margin="57,172,0,0" VerticalAlignment="Top" Height="99" Width="277"                 Background="#FFD52626" FontSize="36" FontFamily="Global User Interface" Click="HelloButton_Click"/> 

Listing 8

Add “Hello, Windows!”

Now, what our application will do is, simply click on the Button control will display the content of TextBox control into the TextBlock control.

The Button click event handler code looks like Listing 9 where we set the Text property of the TextBlock to the Text property value of the TextBox control.

  1. private void HelloButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    HelloTextBlock.Text = HelloTextBox.Text;  

Listing 9

Build and Run

Now let's build and run our application by pressing F5. Your app is deployed and runs. Now type some text in the TextBox and click the Button. You will see the output is displayed in the TextBlock area. See Figure 9.



Figure 9

When you build the app, Visual Studio deploys your app to the Windows Store apps. You will see the HelloWindows app installed. See Figure 10.



Figure 10.

Click or touch on app will launch the app.

Now type some text in the text box and click the “Click Me!” button and see the output.

Summary


Windows Store Apps are the new way to build Windows applications. In this tutorial, we learned how to build our first Windows Store app using Visual Studio 2014.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.