在本章中,我們將討論如何使用.NET Core創(chuàng)建UWP應(yīng)用程序。 UWP也被稱為Windows 10 UWP應(yīng)用程序。 此應(yīng)用程序不能在以前版本的Windows上運行,但只能在未來版本的Windows上運行。
現(xiàn)在按照下面這些步驟來創(chuàng)建并實現(xiàn)一個UWP應(yīng)用程序。
從中央窗格中,選擇空白應(yīng)用程序(通用Windows)模板。
在名稱字段中輸入UWPFirstApp 命名該項目,并選擇存儲目錄,然后單擊【確定】。
出現(xiàn)目標(biāo)版本/最低版本對話框。本教程使用默認(rèn)設(shè)置,所以直接選擇【確定】來創(chuàng)建項目。
在這里,有一個可以針對所有Windows 10設(shè)備的項目,并且您可能會注意到,.NET Core和UWP都是簡化了多重定位。
新項目打開時,其文件顯示在“解決方案資源管理器”窗格的右側(cè)。需要選擇“解決方案資源管理器”選項卡,而不是“屬性”選項卡來查看文件。
創(chuàng)建項目的工作區(qū)如下 -

要查看運行的示例,可以打開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# 代碼。打開MainPage.XAML.cs 并添加下面的代碼。
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";
}
現(xiàn)在在本地機器上運行上面的代碼,點擊菜單:“生成”->生成UWPFirstApp(可能系統(tǒng)會提示要求設(shè)置系統(tǒng)模式,選擇“開發(fā)人員”),然后點擊Visual Studio正上方綠色箭頭的“本地計算機”開始運行,會看到下面的窗口。在文本框中輸入字符串名字,然后點擊:【Click Me】 按鈕。得到以下結(jié)果 -
