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

要查看運(yùn)行的示例,可以打開(kāi)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>
以下是處理按鈕的點(diǎn)擊事件的 C# 代碼。打開(kāi)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)在在本地機(jī)器上運(yùn)行上面的代碼,點(diǎn)擊菜單:“生成”->生成UWPFirstApp(可能系統(tǒng)會(huì)提示要求設(shè)置系統(tǒng)模式,選擇“開(kāi)發(fā)人員”),然后點(diǎn)擊Visual Studio正上方綠色箭頭的“本地計(jì)算機(jī)”開(kāi)始運(yùn)行,會(huì)看到下面的窗口。在文本框中輸入字符串名字,然后點(diǎn)擊:【Click Me】 按鈕。得到以下結(jié)果 -
