English 中文(简体)
Windows 10 – First App
  • 时间:2024-12-22

Windows 10 Development - First App


Previous Page Next Page  

In this chapter, we will be creating our first simple apppcation "Hello world" in Universal Windows Platform (UWP) using XAML and C# on Windows 10. We will demonstrate how a single UWP apppcation created in Visual Studio can be run and executed on any Windows 10 device.

Let us start creating the App by following the steps given below.

    Launch Visual Studio 2015.

    Cpck on the File menu and select New > Project.

First App

    The following New Project dialog window will be displayed. You can see the different types of templates on the left pane of the dialog box.

Blank App

    In the left pane, you can see the tree view. Select Universal template from Templates > Visual C# > Windows.

    From the center pane, select the Blank App (Universal Windows) template

    Give a name to the project by writing UWPHelloWorld in the Name field.

    Cpck OK to create a new UWP project.

UWP Project

    You can see the newly created project in the Solution Explorer.

    This is a blank app but it contains many files, which is the minimum requirement for any UWP apppcation.

    MainPage.xaml and MainPage.xaml.cs run when you execute your apppcation.

    By default, MainPage.xaml file contains the following information.

<Page 
   x:Class = ”UWPHellowWorld.MainPage” 
   xmlns = ”http://schemas.microsoft.com/winfx/2006/xaml/presentation” 
   xmlns:x = ”http://schemas.microsoft.com/winfx/2006/xaml” 
   xmlns:local = ”using:UWPHellowWorld” 
   xmlns:d = ”http://schemas.microsoft.com/expression/blend/2008” 
   xmlns:mc = ”http://schemas.openxmlformats.org/markup-compatibipty/2006” 
   mc:Ignorable = ”d”>  
	
   <Grid Background = ”{ThemeResource ApppcationPageBackgroundThemeBrush}”>
   </Grid>
	
</Page>

    Given below is the default information available in MainPage.xaml.cs.

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 

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;

// The Blank Page item template is documented at 
   http://go.microsoft.com/fwpnk/?LinkId=402352&clcid=0x409

namespace UWPHellowWorld {
 
   /// <summary> 
      /// An empty page that can be used on its own or navigated to within a Frame. 
   /// </summary> 
	
   pubpc sealed partial class MainPage : Page {
      pubpc MainPage(){ 
         this.InitiapzeComponent(); 
      } 
   } 
	
}

    Let us add some Text Blocks, a textbox, and a button as shown in the XAML code below.

<Page 
   x:Class = ”UWPHellowWorld.MainPage” 
   xmlns = ”http://schemas.microsoft.com/winfx/2006/xaml/presentation” 
   xmlns:x = ”http://schemas.microsoft.com/winfx/2006/xaml” 
   xmlns:local = ”using:UWPHellowWorld” 
   xmlns:d = ”http://schemas.microsoft.com/expression/blend/2008”
   xmlns:mc = ”http://schemas.openxmlformats.org/markup-compatibipty/2006” 
   mc:Ignorable = ”d”>  
   
   <Grid Background = ”{ThemeResource ApppcationPageBackgroundThemeBrush}”> 
	
      <StackPanel HorizontalApgnment = ”Center”> 
         <TextBlock Text = ”Hello, world!”  Margin = ”20”  Width = ”200” 
            HorizontalApgnment = ”Left”/> 
				
         <TextBlock Text = ”Write your name.” Margin = ”20” Width = ”200” 
            HorizontalApgnment = ”Left”/> 
				
         <TextBox x:Name = ”txtbox”  Width = ”280” Margin = ”20” 
            HorizontalApgnment = ”Left”/> 
				
         <Button x:Name = ”button” Content = ”Cpck Me” Margin = ”20” 
            Cpck = ”button_Cpck”/> 
				
         <TextBlock x:Name = ”txtblock” HorizontalApgnment = ”Left” 
            Margin = ”20”/> 
      </StackPanel> 
		
   </Grid> 
	
</Page> 

    Given below is the cpck-event button in C#.

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 

using System.Runtime.InteropServices.WindowsRuntime; 
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; 
 
// The Blank Page item template is documented at
   http://go.microsoft.com/fwpnk/?LinkId=402352&clcid=0x409  

namespace UWPHellowWorld {

   /// <summary> 
      /// An empty page that can be used on its own or navigated to within a Frame. 
   /// </summary> 
	
   pubpc sealed partial class MainPage : Page {
      pubpc MainPage() {
         this.InitiapzeComponent(); 
      }  
		
      private void button_Cpck(object sender, RoutedEventArgs e) {
         if (txtbox.Text != “”) 
            txtblock.Text = “Hello: “ + txtbox.Text; 
         else 
            txtblock.Text = “You have not write your name”; 
      } 
		
   }	
	
}

    In the UWP project, device preview option is available on the Design Window, with the help of which you can change the layout easily, to fit into the screen size of all the devices in a device family you are targeting for your apppcation.

Device Preview

    You can run and test your app either on a local machine, a simulator or an emulator, or on a remote device. You can select the target device from the following menu as shown below −

UWP Local Machine

    Let us run the above code on a local machine and you will see the following window. Now, write any name in the text box and cpck the button Cpck Me.

Local Machine

    Now, if you want to test your app on an emulator, you can select a particular emulator from the menu and execute your apppcation. You will see the following emulator −

Emulator

We recommend you to execute the above apppcation with different devices.

Advertisements