Chapter 1 Markup Language and code: Section 1 first Project (1)

Source: Internet
Author: User


Starting from the classic book C Language published by Brian Kernighan and Dennis Ritchie, the use of Hello World code for beginners has become a habit, let's create a similar program for the new world of Windows 8.

I suppose you have installed Windows 8 preview with development tools and sdks. In particular, in this book, I will use Visual Studio to refer to Microsoft Visual Studio Express 2012 RC for Windows 8.

Start Screen startup VS from Windows 8. Let's start writing code.

 

First Project

On the VS screen, the start tag should have been selected. On the left side, you will see an option "Create a new project. Click it, or select "Create a new project" from the File menu ".

When the create new project dialog box is displayed, select Visual C # In the left-side Navigation Pane, create a Windows 8 project, and select a blank application from the middle area. Enter the project name "Hello" at the bottom of the dialog box. Make the solution name the same as this. Use the Browse button to select the local storage location of the program, and then click OK. (When it comes to Visual Studio, I usually call the mouse "click". In this application, I will use the touch term "tap"-"Touch. The Visual Studio version optimized for touch may appear several years later)

Visual Studio creates a solution named Hello. In the solution, there is a project named Hello, and there are a bunch of files in this project. These files are listed one by one in the solution browser on the right of the Visual Studio screen. Each Visual Studio solution has at least one project. One solution may include another application project and library.

The file list of this project contains a file named MainPage. for a file in xaml, if you click the Small Arrow next to the file, you will see a file named MainPage under it. xaml. cs files.

You can double-click or right-click the file to view the two files.

The MainPage. xaml and MainPage. xaml. cs files are linked in the solution browser because they are all used to define a class called MainPage. For simple programs like Hello, this MainPage class defines all the visual user interfaces of this application.

MainPage. xaml. cs has a. cs suffix representing C #. Aside from comments, the MainPage. xaml. cs. file should contain the following C # code:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;namespace Hello{     public sealed partial class MainPage : Page     {          public MainPage()          {             this.InitializeComponent();          }          protected override void OnNavigatedTo(NavigationEventArgs e)          {          }     }}

All the namespaces you need in advance must use the using command to introduce this file. You will find that most of the MainPage. xaml. cs files do not need all of these namespaces, and many other MainPage. xaml. cs files need some additional namespaces.

These namespaces can be divided into two types by using the first Keyword:

System. * new Windows 8 application. net Framework

Windows. * Windows Runtime (WinRT)

According to the list of Using commands, namespaces starting with Windows. UI. Xaml play an important role in Windows runtime.

Next, the MainPage. xaml. cs file defines a namespace named Hello (the same as the project name) and a MainPage class, which inherits the Page class. The Page class is part of the Windows runtime.

The documentation of Windows 8 API is written through the namespace. Therefore, if you want to find a Document of the Page class, it is very useful to understand its defined namespace. Let's move the cursor over the MainPage. xaml. cs source code Page, and you will find that the Page is in the Windows. UI. Xaml. Controls namespace.

There is an InitializeComponent method in the constructor of the MainPage class (I will talk about it later). This class also contains an override method OnNavigatedTo. Windows 8 applications always have a page navigation structure similar to a website. Therefore, they always contain multiple classes that inherit the Page. For the purpose of navigation, the Page class defines some Virtual Methods: OnNavigatingFrom, OnNavigatedFrom, and OnNavigatedTo. This OnNavigatedTo rewrite method is a good place for initialization when the page changes to an active page. However, this is all intended for the future. In the early chapters of this book, most programs only have one page. I will refer more to an application Page instead of a Window. There is still a Window under the application, but it is not as important as a Page.

Note that the MainPage class has a partial keyword. This keyword means that the class definition has other parts in another C # source code file. As you can see, it does. However, in terms of concept, the other part of the MainPage class is not in another C # code, but in the MainPage. xaml file:

<Page      x:Class="Hello.MainPage"      IsTabStop="false"      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      xmlns:local="using:Hello"      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d">      <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">      </Grid></Page>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.