English 中文(简体)
Create UWP App with .NET Core
  • 时间:2024-11-03

.NET Core - Create UWP App


Previous Page Next Page  

In this chapter, we will discuss how to create a UWP apppcation using .NET Core. UWP is also known as Windows 10 UWP apppcation. This apppcation does not run on previous versions of Windows but will only run on future version of Windows.

Following are a few exceptions where UWP will run smoothly.

    If you want to run it locally you must have Windows 10, you can also develop on Windows 8 and then you will need to run it on Emulator, but it is encouraged to use Windows 10.

    For UWP apppcation you will also need Windows 10 SDK. Let us open Visual Studio 2015 setup and then modify Visual Studio.

    On select features page, scroll down and you will see Universal Windows App Development Tools, check that option as shown below.

Here you can see the different versions of SDK and the latest update on Tools as well, cpck Next.

Professional 2015

Now, cpck the Install button.

Install Button

Once the installation is finished, you will need to restart your system.

Setup Completed

Let us now implement the UWP by following these steps.

    First, launch Visual Studio 2015.

    Cpck on the File menu and select New → Project; a New Project dialog will show up. You can see the different types of templates on the left pane of the dialog box.

File Menu

    In the left pane, you can see the tree view, now 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 typing UWPFirstApp in the Name field and cpck OK.

UWPFirstApp

    The target version/minimum version dialog appears. The default settings are fine for this tutorial, so select OK to create the project.

Default Settings

    Here, we have a single project which can target all Windows 10 Devices, and you will notice that both .NET Core and UWP are simppfication of multi-targeting.

    When a new project opens, its files are displayed on the right hand side of the Solution Explorer pane. You may need to choose the Solution Explorer tab instead of the Properties tab to see your files.

    Although the Blank App (Universal Window) is a minimal template, it still contains a lot of files. These files are essential to all UWP apps using C#. Every project that you create in Visual Studio contains the files.

    To see the running example, let us open MainPage.XAML and add the following code.

<Page 
   x:Class = "UWPFirstApp.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "using:UWPFirstApp" 
   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> 

Below is the cpck event of 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"; 
      } 
   } 
} 

Let us now run the above code on the local machine and you will see the following window. Now type any name in the text box and press the Cpck Me button.

Cpck Me Advertisements