在本章中,我们将讨论如何使用.net core创建uwp应用程序。 uwp也被称为windows 10 uwp应用程序。 此应用程序不能在以前版本的windows上运行,但只能在未来版本的windows上运行。
现在按照下面这些步骤来创建并实现一个uwp应用程序。
从中央窗格中,选择空白应用程序(通用windows)模板。
在名称字段中输入uwpfirstapp 命名该项目,并选择存储目录,然后单击【确定】。
出现目标版本/最低版本对话框。本教程使用默认设置,所以直接选择【确定】来创建项目。
在这里,有一个可以针对所有windows 10设备的项目,并且您可能会注意到,.net core和uwp都是简化了多重定位。
新项目打开时,其文件显示在“解决方案资源管理器”窗格的右侧。需要选择“解决方案资源管理器”选项卡,而不是“属性”选项卡来查看文件。
创建项目的工作区如下 -
要查看运行的示例,可以打开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";
}
现在在本地机器上运行上面的代码,点击菜单:“生成”->生成uwpfirstapp(可能系统会提示要求设置系统模式,选择“开发人员”),然后点击visual studio正上方绿色箭头的“本地计算机”开始运行,会看到下面的窗口。在文本框中输入字符串名字,然后点击:【click me】 按钮。得到以下结果 -