.NET Core - 建立 UWP 應用



在本章中,我們將討論如何使用 .NET Core 建立 UWP 應用程式。UWP 也稱為 Windows 10 UWP 應用程式。此應用程式無法在早期版本的 Windows 上執行,而只能在未來版本的 Windows 上執行。

以下是一些 UWP 可以順利執行的例外情況。

  • 如果要本地執行它,則必須擁有 Windows 10,您也可以在 Windows 8 上進行開發,然後需要在模擬器上執行它,但建議使用 Windows 10。

  • 對於 UWP 應用程式,您還需要 Windows 10 SDK。讓我們開啟 Visual Studio 2015 設定,然後修改 Visual Studio。

  • 在“選擇功能”頁面上,向下滾動,您將看到“通用 Windows 應用開發工具”,選中該選項,如下所示。

在這裡,您可以看到 SDK 的不同版本以及工具的最新更新,單擊“下一步”。

Professional 2015

現在,單擊“安裝”按鈕。

Install Button

安裝完成後,您需要重新啟動系統。

Setup Completed

現在讓我們按照以下步驟實現 UWP。

  • 首先,啟動 Visual Studio 2015。

  • 單擊“檔案”選單,然後選擇“新建”→“專案”;將顯示“新建專案”對話方塊。您可以在對話方塊的左側窗格中看到不同型別的模板。

File Menu
  • 在左側窗格中,您可以看到樹形檢視,現在從“模板”→“Visual C#”→“Windows”中選擇“通用”模板。

  • 在中間窗格中,選擇“空白應用(通用 Windows)”模板。

  • 在“名稱”欄位中鍵入UWPFirstApp,為專案命名,然後單擊“確定”。

UWPFirstApp
  • 將出現目標版本/最低版本對話方塊。對於本教程,預設設定是可以的,因此選擇“確定”以建立專案。

Default Settings
  • 在這裡,我們有一個可以針對所有 Windows 10 裝置的單個專案,您會注意到 .NET Core 和 UWP 都是多目標的簡化。

  • 當新專案開啟時,其檔案將顯示在解決方案資源管理器窗格的右側。您可能需要選擇解決方案資源管理器選項卡而不是屬性選項卡才能檢視您的檔案。

  • 儘管“空白應用(通用視窗)”是一個最小的模板,但它仍然包含很多檔案。這些檔案對於所有使用 C# 的 UWP 應用都是必不可少的。您在 Visual Studio 中建立的每個專案都包含這些檔案。

  • 要檢視執行示例,讓我們開啟 MainPage.XAML 並新增以下程式碼。

<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-compatibility/2006" 
   mc:Ignorable = "d">  
   
   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
      <StackPanel HorizontalAlignment = "Center"> 
         <TextBlock Text = "Hello, world!"  
            Margin = "20" 
            Width = "200" 
            HorizontalAlignment = "Left"/> 
         <TextBlock Text = "Write your name." 
            Margin = "20" 
            Width = "200" 
            HorizontalAlignment = "Left"/> 
         <TextBox x:Name = "txtbox"  
            Width = "280" 
            Margin = "20" 
            HorizontalAlignment = "Left"/> 
         <Button x:Name = "button" Content = "Click Me" 
            Margin = "20" 
            Click = "button_Click"/> 
         <TextBlock x:Name = "txtblock"  
            HorizontalAlignment = "Left" 
            Margin = "20"/> 
      </StackPanel> 
   </Grid> 

</Page> 

以下是 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/fwlink/?LinkId=402352&clcid=0x409  

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

現在讓我們在本地計算機上執行以上程式碼,您將看到以下視窗。現在在文字框中輸入任何名稱,然後按“單擊我”按鈕。

Click Me
廣告