翼度科技»论坛 编程开发 .net 查看内容

【.NET 深呼吸】全代码编写WPF程序

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
学习 Code 总有这样一个过程:入门时候比较依赖设计器、标记语言等辅助工具;等到玩熟练了就会发现纯代码写 UI 其实更高效。而且,纯代码编写也是最灵活的。Windows Forms 项目是肯定可以全代码编写的,哪怕你使用了设计器,它最后也是生成代码文件;而 WPF 就值得探索一下了。咱们知道,WPF 使用 XAML 标记来构建 UI 部分。由于 XAML 扩展了许多功能,用起来自然比 HTML 舒服。但是,老周向来不喜欢标记语言,这也是我向来不喜欢搞前端的原因。尽管某些前端框架模仿 WPF 也搞出数据绑定、MVVM、数据模板之类的名堂,也很难说用得特舒服。
有很多中小型项目都会把 Web 前端部分外包出去,尤其是给私人做——比如一两个人或两三个人做,也不外给其他公司做。有些人总以为前端很火(这里头媒体造势的功劳不小),可往深层一挖,那可不一定了。Web 其实只做了一套 UI 罢了,后端许多是通用模型,既可以和 Web 前端对接,也可以和桌面前端对接,B/S、C/S 通杀的项目也不少。
很多行业软件,如工业医疗,甚至财务、进销存等,还是用成熟的技术好,尤其是桌面技术体验更佳。当然有些行业软件也有 Web UI,纯辅助,一般就是看看报表看看大图查查订单而已。生产力悠关的东西,你还得相信桌面的魅力,娱乐相关的就随便。当然也有用 Web 技术开发桌面UI的框架,这些东西能用但效果不算好,尤其是性能。老周这里说的性能只是要求较宽的性能,而不是苛刻要求下的性能。啥意思呢,就是说用 Web 技术做桌面程序,存在性能问题不需要专用工具测,肉眼就能感觉到严重的性能问题了——吃内存特大,占CPU有亿点高(虽说占得不算恐怖)。这里所说的性能问题要排除 VS Code,因为这货是个奇葩,性能表现挺好。
许多人容易被表面现象迷惑,比如认为招聘信息多的就以为很吃香。那可不一定,有些技术,招聘少并不代表用的人少。老在招聘的顶多说明这些岗位流动性大,这个公司的员工热爱跳槽罢了。近年来 Python 被“利益携带体”们炒得可热了,甚至一些新手以为 Python 是刚出来的新语言。你想多了,就算没有 C 语言早(1972),那也是 80 年代末的东东了。我在学 Python 的时候,估计某些小菜鸟还没出生呢。不要听那些培训班胡说八道,它们的目的是你的钱包,而不是你的码农生涯,它们说话从来不需要负责的。如果你除了 Python 什么都不会的话,那除了会写点“脚毛”外,你什么也干不成。不管你想玩人工智障、视觉神经还是别的东西,你得掌握C语言,特别是想搞更底层的。只会 Py 没准连工作都难找,更别说年薪 500W 越南币了。
在你不知道的领域,你可曾想象,VB6、易语言、Delphi、MFC 还有不少人在用呢。告诉你个秘密,学好汇编可能更吃香,以后会这个的人更少了。信不信由你。当然,积极学习新东西是没错的,这也老周一向主张。不过,你同时得清楚,许多技术之间并不存在相互替代的关系,只不过是你做什么样的程序,就用什么样的技术罢了。比如这桌面程序,你不用纠结,很简单:考虑跨平台的,首选 Qt;仅考虑 Windows 的,那多了去,随便,当然,微软自家自然是最合适的。
可是,一些脑子太灵活的人又纠结了,我选了 Qt,那我用 Widgets 做还是用 QML 做?我选了.NET,那我用 Windows Forms 还是 WPF?还是 MAUI ?对于这种问题,老周送你一句:“像你们这种人是没法改变的,只有滚出码农界”。
好了,上面扯了几段“废腑”之话,回归正题,咱们讨论 WPF,老周这里说的是完全用代码写,指的是一行 XAML 都没有。当然,大伙伴们肯定说那没问题的。构建常规界面绝对行得通,但遇到像数据模板、控件模板、资源字典这些,就得费一点点代码。虽然网上能找到几位同道中人写的小作文,但要么版本太旧,要么过于粗糙。于是老周逮住了这个机会,可以瞎扯蛋一回了。
前文多次强调,咱们就纯代码写 WPF 的,无一行 XAML。所以,默认的 WPF 项目模板咱们就不用了。咱们用控制台应用的模板就行了。来,动手练习一下。
首先,创建一个控制台项目。
  1. dotnet new console -n MyApp -o .
复制代码
dotnet new 命令知道乎?嗯,用来创建项目的,然后是项目模板的名称,console 表示控制台应用程序。模板名字我记不住哟。记它干吗,执行一下下面这一句就能看各种模板了:
  1. dotnet new list
复制代码
这里你可别理解歪了,它不是说用名叫 list 的模板创建项目啊,list 是列出可用的项目模板。然后,你能得到这个表:
  1. 模板名<NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope><NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope><NameScope.NameScope>
  6.     <NameScope />
  7. </NameScope.NameScope><NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope><NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope><NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope><NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope><NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>   短名称<NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope><NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope><NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope>   语言<NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope><NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope>标记
  28. ---------------------------------------  -------------------  ----------  --------------------------------
  29. ASP.NET Core gRPC 服务<NameScope.NameScope>
  30.     <NameScope />
  31. </NameScope.NameScope><NameScope.NameScope>
  32.     <NameScope />
  33. </NameScope.NameScope><NameScope.NameScope>
  34.     <NameScope />
  35. </NameScope.NameScope><NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope>   grpc<NameScope.NameScope>
  38.     <NameScope />
  39. </NameScope.NameScope><NameScope.NameScope>
  40.     <NameScope />
  41. </NameScope.NameScope><NameScope.NameScope>
  42.     <NameScope />
  43. </NameScope.NameScope><NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope> [C#]<NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope><NameScope.NameScope>
  48.     <NameScope />
  49. </NameScope.NameScope>Web/gRPC
  50. ASP.NET Core Web API<NameScope.NameScope>
  51.     <NameScope />
  52. </NameScope.NameScope><NameScope.NameScope>
  53.     <NameScope />
  54. </NameScope.NameScope><NameScope.NameScope>
  55.     <NameScope />
  56. </NameScope.NameScope><NameScope.NameScope>
  57.     <NameScope />
  58. </NameScope.NameScope><NameScope.NameScope>
  59.     <NameScope />
  60. </NameScope.NameScope> webapi<NameScope.NameScope>
  61.     <NameScope />
  62. </NameScope.NameScope><NameScope.NameScope>
  63.     <NameScope />
  64. </NameScope.NameScope><NameScope.NameScope>
  65.     <NameScope />
  66. </NameScope.NameScope>   [C#],F#<NameScope.NameScope>
  67.     <NameScope />
  68. </NameScope.NameScope> Web/WebAPI
  69. ASP.NET Core Web 应用<NameScope.NameScope>
  70.     <NameScope />
  71. </NameScope.NameScope><NameScope.NameScope>
  72.     <NameScope />
  73. </NameScope.NameScope><NameScope.NameScope>
  74.     <NameScope />
  75. </NameScope.NameScope><NameScope.NameScope>
  76.     <NameScope />
  77. </NameScope.NameScope><NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope>webapp,razor<NameScope.NameScope>
  80.     <NameScope />
  81. </NameScope.NameScope><NameScope.NameScope>
  82.     <NameScope />
  83. </NameScope.NameScope> [C#]<NameScope.NameScope>
  84.     <NameScope />
  85. </NameScope.NameScope><NameScope.NameScope>
  86.     <NameScope />
  87. </NameScope.NameScope>Web/MVC/Razor Pages
  88. ASP.NET Core Web 应用(模型-视图-控制器)  mvc<NameScope.NameScope>
  89.     <NameScope />
  90. </NameScope.NameScope><NameScope.NameScope>
  91.     <NameScope />
  92. </NameScope.NameScope><NameScope.NameScope>
  93.     <NameScope />
  94. </NameScope.NameScope><NameScope.NameScope>
  95.     <NameScope />
  96. </NameScope.NameScope>  [C#],F#<NameScope.NameScope>
  97.     <NameScope />
  98. </NameScope.NameScope> Web/MVC
  99. ASP.NET Core 与 Angular<NameScope.NameScope>
  100.     <NameScope />
  101. </NameScope.NameScope><NameScope.NameScope>
  102.     <NameScope />
  103. </NameScope.NameScope><NameScope.NameScope>
  104.     <NameScope />
  105. </NameScope.NameScope><NameScope.NameScope>
  106.     <NameScope />
  107. </NameScope.NameScope>  angular<NameScope.NameScope>
  108.     <NameScope />
  109. </NameScope.NameScope><NameScope.NameScope>
  110.     <NameScope />
  111. </NameScope.NameScope><NameScope.NameScope>
  112.     <NameScope />
  113. </NameScope.NameScope>  [C#]<NameScope.NameScope>
  114.     <NameScope />
  115. </NameScope.NameScope><NameScope.NameScope>
  116.     <NameScope />
  117. </NameScope.NameScope>Web/MVC/SPA
  118. ASP.NET Core 与 React.js<NameScope.NameScope>
  119.     <NameScope />
  120. </NameScope.NameScope><NameScope.NameScope>
  121.     <NameScope />
  122. </NameScope.NameScope><NameScope.NameScope>
  123.     <NameScope />
  124. </NameScope.NameScope><NameScope.NameScope>
  125.     <NameScope />
  126. </NameScope.NameScope> react<NameScope.NameScope>
  127.     <NameScope />
  128. </NameScope.NameScope><NameScope.NameScope>
  129.     <NameScope />
  130. </NameScope.NameScope><NameScope.NameScope>
  131.     <NameScope />
  132. </NameScope.NameScope><NameScope.NameScope>
  133.     <NameScope />
  134. </NameScope.NameScope>[C#]<NameScope.NameScope>
  135.     <NameScope />
  136. </NameScope.NameScope><NameScope.NameScope>
  137.     <NameScope />
  138. </NameScope.NameScope>Web/MVC/SPA
  139. ASP.NET Core 空<NameScope.NameScope>
  140.     <NameScope />
  141. </NameScope.NameScope><NameScope.NameScope>
  142.     <NameScope />
  143. </NameScope.NameScope><NameScope.NameScope>
  144.     <NameScope />
  145. </NameScope.NameScope><NameScope.NameScope>
  146.     <NameScope />
  147. </NameScope.NameScope><NameScope.NameScope>
  148.     <NameScope />
  149. </NameScope.NameScope><NameScope.NameScope>
  150.     <NameScope />
  151. </NameScope.NameScope>  web<NameScope.NameScope>
  152.     <NameScope />
  153. </NameScope.NameScope><NameScope.NameScope>
  154.     <NameScope />
  155. </NameScope.NameScope><NameScope.NameScope>
  156.     <NameScope />
  157. </NameScope.NameScope><NameScope.NameScope>
  158.     <NameScope />
  159. </NameScope.NameScope>  [C#],F#<NameScope.NameScope>
  160.     <NameScope />
  161. </NameScope.NameScope> Web/Empty
  162. Blazor Server 应用<NameScope.NameScope>
  163.     <NameScope />
  164. </NameScope.NameScope><NameScope.NameScope>
  165.     <NameScope />
  166. </NameScope.NameScope><NameScope.NameScope>
  167.     <NameScope />
  168. </NameScope.NameScope><NameScope.NameScope>
  169.     <NameScope />
  170. </NameScope.NameScope><NameScope.NameScope>
  171.     <NameScope />
  172. </NameScope.NameScope>   blazorserver<NameScope.NameScope>
  173.     <NameScope />
  174. </NameScope.NameScope><NameScope.NameScope>
  175.     <NameScope />
  176. </NameScope.NameScope> [C#]<NameScope.NameScope>
  177.     <NameScope />
  178. </NameScope.NameScope><NameScope.NameScope>
  179.     <NameScope />
  180. </NameScope.NameScope>Web/Blazor
  181. Blazor Server 应用空<NameScope.NameScope>
  182.     <NameScope />
  183. </NameScope.NameScope><NameScope.NameScope>
  184.     <NameScope />
  185. </NameScope.NameScope><NameScope.NameScope>
  186.     <NameScope />
  187. </NameScope.NameScope><NameScope.NameScope>
  188.     <NameScope />
  189. </NameScope.NameScope><NameScope.NameScope>
  190.     <NameScope />
  191. </NameScope.NameScope> blazorserver-empty   [C#]<NameScope.NameScope>
  192.     <NameScope />
  193. </NameScope.NameScope><NameScope.NameScope>
  194.     <NameScope />
  195. </NameScope.NameScope>Web/Blazor/Empty
  196. Blazor WebAssembly 应用<NameScope.NameScope>
  197.     <NameScope />
  198. </NameScope.NameScope><NameScope.NameScope>
  199.     <NameScope />
  200. </NameScope.NameScope><NameScope.NameScope>
  201.     <NameScope />
  202. </NameScope.NameScope><NameScope.NameScope>
  203.     <NameScope />
  204. </NameScope.NameScope>  blazorwasm<NameScope.NameScope>
  205.     <NameScope />
  206. </NameScope.NameScope><NameScope.NameScope>
  207.     <NameScope />
  208. </NameScope.NameScope>   [C#]<NameScope.NameScope>
  209.     <NameScope />
  210. </NameScope.NameScope><NameScope.NameScope>
  211.     <NameScope />
  212. </NameScope.NameScope>Web/Blazor/WebAssembly/PWA
  213. Blazor WebAssembly 应用空<NameScope.NameScope>
  214.     <NameScope />
  215. </NameScope.NameScope><NameScope.NameScope>
  216.     <NameScope />
  217. </NameScope.NameScope><NameScope.NameScope>
  218.     <NameScope />
  219. </NameScope.NameScope><NameScope.NameScope>
  220.     <NameScope />
  221. </NameScope.NameScope>blazorwasm-empty<NameScope.NameScope>
  222.     <NameScope />
  223. </NameScope.NameScope> [C#]<NameScope.NameScope>
  224.     <NameScope />
  225. </NameScope.NameScope><NameScope.NameScope>
  226.     <NameScope />
  227. </NameScope.NameScope>Web/Blazor/WebAssembly/PWA/Empty
  228. dotnet gitignore 文件<NameScope.NameScope>
  229.     <NameScope />
  230. </NameScope.NameScope><NameScope.NameScope>
  231.     <NameScope />
  232. </NameScope.NameScope><NameScope.NameScope>
  233.     <NameScope />
  234. </NameScope.NameScope><NameScope.NameScope>
  235.     <NameScope />
  236. </NameScope.NameScope><NameScope.NameScope>
  237.     <NameScope />
  238. </NameScope.NameScope>gitignore<NameScope.NameScope>
  239.     <NameScope />
  240. </NameScope.NameScope><NameScope.NameScope>
  241.     <NameScope />
  242. </NameScope.NameScope><NameScope.NameScope>
  243.     <NameScope />
  244. </NameScope.NameScope><NameScope.NameScope>
  245.     <NameScope />
  246. </NameScope.NameScope><NameScope.NameScope>
  247.     <NameScope />
  248. </NameScope.NameScope><NameScope.NameScope>
  249.     <NameScope />
  250. </NameScope.NameScope>Config
  251. Dotnet 本地工具清单文件<NameScope.NameScope>
  252.     <NameScope />
  253. </NameScope.NameScope><NameScope.NameScope>
  254.     <NameScope />
  255. </NameScope.NameScope><NameScope.NameScope>
  256.     <NameScope />
  257. </NameScope.NameScope><NameScope.NameScope>
  258.     <NameScope />
  259. </NameScope.NameScope>  tool-manifest<NameScope.NameScope>
  260.     <NameScope />
  261. </NameScope.NameScope><NameScope.NameScope>
  262.     <NameScope />
  263. </NameScope.NameScope><NameScope.NameScope>
  264.     <NameScope />
  265. </NameScope.NameScope><NameScope.NameScope>
  266.     <NameScope />
  267. </NameScope.NameScope><NameScope.NameScope>
  268.     <NameScope />
  269. </NameScope.NameScope>Config
  270. EditorConfig 文件<NameScope.NameScope>
  271.     <NameScope />
  272. </NameScope.NameScope><NameScope.NameScope>
  273.     <NameScope />
  274. </NameScope.NameScope><NameScope.NameScope>
  275.     <NameScope />
  276. </NameScope.NameScope><NameScope.NameScope>
  277.     <NameScope />
  278. </NameScope.NameScope><NameScope.NameScope>
  279.     <NameScope />
  280. </NameScope.NameScope><NameScope.NameScope>
  281.     <NameScope />
  282. </NameScope.NameScope>editorconfig<NameScope.NameScope>
  283.     <NameScope />
  284. </NameScope.NameScope><NameScope.NameScope>
  285.     <NameScope />
  286. </NameScope.NameScope><NameScope.NameScope>
  287.     <NameScope />
  288. </NameScope.NameScope><NameScope.NameScope>
  289.     <NameScope />
  290. </NameScope.NameScope><NameScope.NameScope>
  291.     <NameScope />
  292. </NameScope.NameScope> Config
  293. global.json file<NameScope.NameScope>
  294.     <NameScope />
  295. </NameScope.NameScope><NameScope.NameScope>
  296.     <NameScope />
  297. </NameScope.NameScope><NameScope.NameScope>
  298.     <NameScope />
  299. </NameScope.NameScope><NameScope.NameScope>
  300.     <NameScope />
  301. </NameScope.NameScope><NameScope.NameScope>
  302.     <NameScope />
  303. </NameScope.NameScope><NameScope.NameScope>
  304.     <NameScope />
  305. </NameScope.NameScope> globaljson<NameScope.NameScope>
  306.     <NameScope />
  307. </NameScope.NameScope><NameScope.NameScope>
  308.     <NameScope />
  309. </NameScope.NameScope><NameScope.NameScope>
  310.     <NameScope />
  311. </NameScope.NameScope><NameScope.NameScope>
  312.     <NameScope />
  313. </NameScope.NameScope><NameScope.NameScope>
  314.     <NameScope />
  315. </NameScope.NameScope>   Config
  316. MSBuild Directory.Build.props 文件<NameScope.NameScope>
  317.     <NameScope />
  318. </NameScope.NameScope>   buildprops<NameScope.NameScope>
  319.     <NameScope />
  320. </NameScope.NameScope><NameScope.NameScope>
  321.     <NameScope />
  322. </NameScope.NameScope><NameScope.NameScope>
  323.     <NameScope />
  324. </NameScope.NameScope><NameScope.NameScope>
  325.     <NameScope />
  326. </NameScope.NameScope><NameScope.NameScope>
  327.     <NameScope />
  328. </NameScope.NameScope>   MSBuild/props
  329. MSBuild Directory.Build.targets 文件<NameScope.NameScope>
  330.     <NameScope />
  331. </NameScope.NameScope> buildtargets<NameScope.NameScope>
  332.     <NameScope />
  333. </NameScope.NameScope><NameScope.NameScope>
  334.     <NameScope />
  335. </NameScope.NameScope><NameScope.NameScope>
  336.     <NameScope />
  337. </NameScope.NameScope><NameScope.NameScope>
  338.     <NameScope />
  339. </NameScope.NameScope><NameScope.NameScope>
  340.     <NameScope />
  341. </NameScope.NameScope> MSBuild/props
  342. MSTest Test Project<NameScope.NameScope>
  343.     <NameScope />
  344. </NameScope.NameScope><NameScope.NameScope>
  345.     <NameScope />
  346. </NameScope.NameScope><NameScope.NameScope>
  347.     <NameScope />
  348. </NameScope.NameScope><NameScope.NameScope>
  349.     <NameScope />
  350. </NameScope.NameScope><NameScope.NameScope>
  351.     <NameScope />
  352. </NameScope.NameScope>  mstest<NameScope.NameScope>
  353.     <NameScope />
  354. </NameScope.NameScope><NameScope.NameScope>
  355.     <NameScope />
  356. </NameScope.NameScope><NameScope.NameScope>
  357.     <NameScope />
  358. </NameScope.NameScope>   [C#],F#,VB  Test/MSTest
  359. MVC ViewImports<NameScope.NameScope>
  360.     <NameScope />
  361. </NameScope.NameScope><NameScope.NameScope>
  362.     <NameScope />
  363. </NameScope.NameScope><NameScope.NameScope>
  364.     <NameScope />
  365. </NameScope.NameScope><NameScope.NameScope>
  366.     <NameScope />
  367. </NameScope.NameScope><NameScope.NameScope>
  368.     <NameScope />
  369. </NameScope.NameScope><NameScope.NameScope>
  370.     <NameScope />
  371. </NameScope.NameScope>  viewimports<NameScope.NameScope>
  372.     <NameScope />
  373. </NameScope.NameScope><NameScope.NameScope>
  374.     <NameScope />
  375. </NameScope.NameScope>  [C#]<NameScope.NameScope>
  376.     <NameScope />
  377. </NameScope.NameScope><NameScope.NameScope>
  378.     <NameScope />
  379. </NameScope.NameScope>Web/ASP.NET
  380. MVC ViewStart<NameScope.NameScope>
  381.     <NameScope />
  382. </NameScope.NameScope><NameScope.NameScope>
  383.     <NameScope />
  384. </NameScope.NameScope><NameScope.NameScope>
  385.     <NameScope />
  386. </NameScope.NameScope><NameScope.NameScope>
  387.     <NameScope />
  388. </NameScope.NameScope><NameScope.NameScope>
  389.     <NameScope />
  390. </NameScope.NameScope><NameScope.NameScope>
  391.     <NameScope />
  392. </NameScope.NameScope><NameScope.NameScope>
  393.     <NameScope />
  394. </NameScope.NameScope>viewstart<NameScope.NameScope>
  395.     <NameScope />
  396. </NameScope.NameScope><NameScope.NameScope>
  397.     <NameScope />
  398. </NameScope.NameScope><NameScope.NameScope>
  399.     <NameScope />
  400. </NameScope.NameScope>[C#]<NameScope.NameScope>
  401.     <NameScope />
  402. </NameScope.NameScope><NameScope.NameScope>
  403.     <NameScope />
  404. </NameScope.NameScope>Web/ASP.NET
  405. NuGet 配置<NameScope.NameScope>
  406.     <NameScope />
  407. </NameScope.NameScope><NameScope.NameScope>
  408.     <NameScope />
  409. </NameScope.NameScope><NameScope.NameScope>
  410.     <NameScope />
  411. </NameScope.NameScope><NameScope.NameScope>
  412.     <NameScope />
  413. </NameScope.NameScope><NameScope.NameScope>
  414.     <NameScope />
  415. </NameScope.NameScope><NameScope.NameScope>
  416.     <NameScope />
  417. </NameScope.NameScope><NameScope.NameScope>
  418.     <NameScope />
  419. </NameScope.NameScope>   nugetconfig<NameScope.NameScope>
  420.     <NameScope />
  421. </NameScope.NameScope><NameScope.NameScope>
  422.     <NameScope />
  423. </NameScope.NameScope><NameScope.NameScope>
  424.     <NameScope />
  425. </NameScope.NameScope><NameScope.NameScope>
  426.     <NameScope />
  427. </NameScope.NameScope><NameScope.NameScope>
  428.     <NameScope />
  429. </NameScope.NameScope>  Config
  430. NUnit 3 Test Item<NameScope.NameScope>
  431.     <NameScope />
  432. </NameScope.NameScope><NameScope.NameScope>
  433.     <NameScope />
  434. </NameScope.NameScope><NameScope.NameScope>
  435.     <NameScope />
  436. </NameScope.NameScope><NameScope.NameScope>
  437.     <NameScope />
  438. </NameScope.NameScope><NameScope.NameScope>
  439.     <NameScope />
  440. </NameScope.NameScope><NameScope.NameScope>
  441.     <NameScope />
  442. </NameScope.NameScope>nunit-test<NameScope.NameScope>
  443.     <NameScope />
  444. </NameScope.NameScope><NameScope.NameScope>
  445.     <NameScope />
  446. </NameScope.NameScope>   [C#],F#,VB  Test/NUnit
  447. NUnit 3 Test Project<NameScope.NameScope>
  448.     <NameScope />
  449. </NameScope.NameScope><NameScope.NameScope>
  450.     <NameScope />
  451. </NameScope.NameScope><NameScope.NameScope>
  452.     <NameScope />
  453. </NameScope.NameScope><NameScope.NameScope>
  454.     <NameScope />
  455. </NameScope.NameScope><NameScope.NameScope>
  456.     <NameScope />
  457. </NameScope.NameScope> nunit<NameScope.NameScope>
  458.     <NameScope />
  459. </NameScope.NameScope><NameScope.NameScope>
  460.     <NameScope />
  461. </NameScope.NameScope><NameScope.NameScope>
  462.     <NameScope />
  463. </NameScope.NameScope><NameScope.NameScope>
  464.     <NameScope />
  465. </NameScope.NameScope>[C#],F#,VB  Test/NUnit
  466. Razor 类库<NameScope.NameScope>
  467.     <NameScope />
  468. </NameScope.NameScope><NameScope.NameScope>
  469.     <NameScope />
  470. </NameScope.NameScope><NameScope.NameScope>
  471.     <NameScope />
  472. </NameScope.NameScope><NameScope.NameScope>
  473.     <NameScope />
  474. </NameScope.NameScope><NameScope.NameScope>
  475.     <NameScope />
  476. </NameScope.NameScope><NameScope.NameScope>
  477.     <NameScope />
  478. </NameScope.NameScope><NameScope.NameScope>
  479.     <NameScope />
  480. </NameScope.NameScope>   razorclasslib<NameScope.NameScope>
  481.     <NameScope />
  482. </NameScope.NameScope><NameScope.NameScope>
  483.     <NameScope />
  484. </NameScope.NameScope>[C#]<NameScope.NameScope>
  485.     <NameScope />
  486. </NameScope.NameScope><NameScope.NameScope>
  487.     <NameScope />
  488. </NameScope.NameScope>Web/Razor/Library
  489. Razor 组件<NameScope.NameScope>
  490.     <NameScope />
  491. </NameScope.NameScope><NameScope.NameScope>
  492.     <NameScope />
  493. </NameScope.NameScope><NameScope.NameScope>
  494.     <NameScope />
  495. </NameScope.NameScope><NameScope.NameScope>
  496.     <NameScope />
  497. </NameScope.NameScope><NameScope.NameScope>
  498.     <NameScope />
  499. </NameScope.NameScope><NameScope.NameScope>
  500.     <NameScope />
  501. </NameScope.NameScope><NameScope.NameScope>
  502.     <NameScope />
  503. </NameScope.NameScope>   razorcomponent<NameScope.NameScope>
  504.     <NameScope />
  505. </NameScope.NameScope>   [C#]<NameScope.NameScope>
  506.     <NameScope />
  507. </NameScope.NameScope><NameScope.NameScope>
  508.     <NameScope />
  509. </NameScope.NameScope>Web/ASP.NET
  510. Razor 页面<NameScope.NameScope>
  511.     <NameScope />
  512. </NameScope.NameScope><NameScope.NameScope>
  513.     <NameScope />
  514. </NameScope.NameScope><NameScope.NameScope>
  515.     <NameScope />
  516. </NameScope.NameScope><NameScope.NameScope>
  517.     <NameScope />
  518. </NameScope.NameScope><NameScope.NameScope>
  519.     <NameScope />
  520. </NameScope.NameScope><NameScope.NameScope>
  521.     <NameScope />
  522. </NameScope.NameScope><NameScope.NameScope>
  523.     <NameScope />
  524. </NameScope.NameScope>   page<NameScope.NameScope>
  525.     <NameScope />
  526. </NameScope.NameScope><NameScope.NameScope>
  527.     <NameScope />
  528. </NameScope.NameScope><NameScope.NameScope>
  529.     <NameScope />
  530. </NameScope.NameScope><NameScope.NameScope>
  531.     <NameScope />
  532. </NameScope.NameScope> [C#]<NameScope.NameScope>
  533.     <NameScope />
  534. </NameScope.NameScope><NameScope.NameScope>
  535.     <NameScope />
  536. </NameScope.NameScope>Web/ASP.NET
  537. Web 配置<NameScope.NameScope>
  538.     <NameScope />
  539. </NameScope.NameScope><NameScope.NameScope>
  540.     <NameScope />
  541. </NameScope.NameScope><NameScope.NameScope>
  542.     <NameScope />
  543. </NameScope.NameScope><NameScope.NameScope>
  544.     <NameScope />
  545. </NameScope.NameScope><NameScope.NameScope>
  546.     <NameScope />
  547. </NameScope.NameScope><NameScope.NameScope>
  548.     <NameScope />
  549. </NameScope.NameScope><NameScope.NameScope>
  550.     <NameScope />
  551. </NameScope.NameScope><NameScope.NameScope>
  552.     <NameScope />
  553. </NameScope.NameScope> webconfig<NameScope.NameScope>
  554.     <NameScope />
  555. </NameScope.NameScope><NameScope.NameScope>
  556.     <NameScope />
  557. </NameScope.NameScope><NameScope.NameScope>
  558.     <NameScope />
  559. </NameScope.NameScope><NameScope.NameScope>
  560.     <NameScope />
  561. </NameScope.NameScope><NameScope.NameScope>
  562.     <NameScope />
  563. </NameScope.NameScope><NameScope.NameScope>
  564.     <NameScope />
  565. </NameScope.NameScope>Config
  566. Windows 窗体应用<NameScope.NameScope>
  567.     <NameScope />
  568. </NameScope.NameScope><NameScope.NameScope>
  569.     <NameScope />
  570. </NameScope.NameScope><NameScope.NameScope>
  571.     <NameScope />
  572. </NameScope.NameScope><NameScope.NameScope>
  573.     <NameScope />
  574. </NameScope.NameScope><NameScope.NameScope>
  575.     <NameScope />
  576. </NameScope.NameScope><NameScope.NameScope>
  577.     <NameScope />
  578. </NameScope.NameScope> winforms<NameScope.NameScope>
  579.     <NameScope />
  580. </NameScope.NameScope><NameScope.NameScope>
  581.     <NameScope />
  582. </NameScope.NameScope><NameScope.NameScope>
  583.     <NameScope />
  584. </NameScope.NameScope> [C#],VB<NameScope.NameScope>
  585.     <NameScope />
  586. </NameScope.NameScope> Common/WinForms
  587. Windows 窗体控件库<NameScope.NameScope>
  588.     <NameScope />
  589. </NameScope.NameScope><NameScope.NameScope>
  590.     <NameScope />
  591. </NameScope.NameScope><NameScope.NameScope>
  592.     <NameScope />
  593. </NameScope.NameScope><NameScope.NameScope>
  594.     <NameScope />
  595. </NameScope.NameScope><NameScope.NameScope>
  596.     <NameScope />
  597. </NameScope.NameScope>   winformscontrollib   [C#],VB<NameScope.NameScope>
  598.     <NameScope />
  599. </NameScope.NameScope> Common/WinForms
  600. Windows 窗体类库<NameScope.NameScope>
  601.     <NameScope />
  602. </NameScope.NameScope><NameScope.NameScope>
  603.     <NameScope />
  604. </NameScope.NameScope><NameScope.NameScope>
  605.     <NameScope />
  606. </NameScope.NameScope><NameScope.NameScope>
  607.     <NameScope />
  608. </NameScope.NameScope><NameScope.NameScope>
  609.     <NameScope />
  610. </NameScope.NameScope><NameScope.NameScope>
  611.     <NameScope />
  612. </NameScope.NameScope> winformslib<NameScope.NameScope>
  613.     <NameScope />
  614. </NameScope.NameScope><NameScope.NameScope>
  615.     <NameScope />
  616. </NameScope.NameScope>  [C#],VB<NameScope.NameScope>
  617.     <NameScope />
  618. </NameScope.NameScope> Common/WinForms
  619. WPF 应用程序<NameScope.NameScope>
  620.     <NameScope />
  621. </NameScope.NameScope><NameScope.NameScope>
  622.     <NameScope />
  623. </NameScope.NameScope><NameScope.NameScope>
  624.     <NameScope />
  625. </NameScope.NameScope><NameScope.NameScope>
  626.     <NameScope />
  627. </NameScope.NameScope><NameScope.NameScope>
  628.     <NameScope />
  629. </NameScope.NameScope><NameScope.NameScope>
  630.     <NameScope />
  631. </NameScope.NameScope><NameScope.NameScope>
  632.     <NameScope />
  633. </NameScope.NameScope> wpf<NameScope.NameScope>
  634.     <NameScope />
  635. </NameScope.NameScope><NameScope.NameScope>
  636.     <NameScope />
  637. </NameScope.NameScope><NameScope.NameScope>
  638.     <NameScope />
  639. </NameScope.NameScope><NameScope.NameScope>
  640.     <NameScope />
  641. </NameScope.NameScope>  [C#],VB<NameScope.NameScope>
  642.     <NameScope />
  643. </NameScope.NameScope> Common/WPF
  644. WPF 用户控件库<NameScope.NameScope>
  645.     <NameScope />
  646. </NameScope.NameScope><NameScope.NameScope>
  647.     <NameScope />
  648. </NameScope.NameScope><NameScope.NameScope>
  649.     <NameScope />
  650. </NameScope.NameScope><NameScope.NameScope>
  651.     <NameScope />
  652. </NameScope.NameScope><NameScope.NameScope>
  653.     <NameScope />
  654. </NameScope.NameScope><NameScope.NameScope>
  655.     <NameScope />
  656. </NameScope.NameScope>   wpfusercontrollib<NameScope.NameScope>
  657.     <NameScope />
  658. </NameScope.NameScope>[C#],VB<NameScope.NameScope>
  659.     <NameScope />
  660. </NameScope.NameScope> Common/WPF
  661. WPF 类库<NameScope.NameScope>
  662.     <NameScope />
  663. </NameScope.NameScope><NameScope.NameScope>
  664.     <NameScope />
  665. </NameScope.NameScope><NameScope.NameScope>
  666.     <NameScope />
  667. </NameScope.NameScope><NameScope.NameScope>
  668.     <NameScope />
  669. </NameScope.NameScope><NameScope.NameScope>
  670.     <NameScope />
  671. </NameScope.NameScope><NameScope.NameScope>
  672.     <NameScope />
  673. </NameScope.NameScope><NameScope.NameScope>
  674.     <NameScope />
  675. </NameScope.NameScope><NameScope.NameScope>
  676.     <NameScope />
  677. </NameScope.NameScope> wpflib<NameScope.NameScope>
  678.     <NameScope />
  679. </NameScope.NameScope><NameScope.NameScope>
  680.     <NameScope />
  681. </NameScope.NameScope><NameScope.NameScope>
  682.     <NameScope />
  683. </NameScope.NameScope>   [C#],VB<NameScope.NameScope>
  684.     <NameScope />
  685. </NameScope.NameScope> Common/WPF
  686. WPF 自定义控件库<NameScope.NameScope>
  687.     <NameScope />
  688. </NameScope.NameScope><NameScope.NameScope>
  689.     <NameScope />
  690. </NameScope.NameScope><NameScope.NameScope>
  691.     <NameScope />
  692. </NameScope.NameScope><NameScope.NameScope>
  693.     <NameScope />
  694. </NameScope.NameScope><NameScope.NameScope>
  695.     <NameScope />
  696. </NameScope.NameScope><NameScope.NameScope>
  697.     <NameScope />
  698. </NameScope.NameScope> wpfcustomcontrollib  [C#],VB<NameScope.NameScope>
  699.     <NameScope />
  700. </NameScope.NameScope> Common/WPF
  701. xUnit Test Project<NameScope.NameScope>
  702.     <NameScope />
  703. </NameScope.NameScope><NameScope.NameScope>
  704.     <NameScope />
  705. </NameScope.NameScope><NameScope.NameScope>
  706.     <NameScope />
  707. </NameScope.NameScope><NameScope.NameScope>
  708.     <NameScope />
  709. </NameScope.NameScope><NameScope.NameScope>
  710.     <NameScope />
  711. </NameScope.NameScope>   xunit<NameScope.NameScope>
  712.     <NameScope />
  713. </NameScope.NameScope><NameScope.NameScope>
  714.     <NameScope />
  715. </NameScope.NameScope><NameScope.NameScope>
  716.     <NameScope />
  717. </NameScope.NameScope><NameScope.NameScope>
  718.     <NameScope />
  719. </NameScope.NameScope>[C#],F#,VB  Test/xUnit
  720. 协议缓冲区文件<NameScope.NameScope>
  721.     <NameScope />
  722. </NameScope.NameScope><NameScope.NameScope>
  723.     <NameScope />
  724. </NameScope.NameScope><NameScope.NameScope>
  725.     <NameScope />
  726. </NameScope.NameScope><NameScope.NameScope>
  727.     <NameScope />
  728. </NameScope.NameScope><NameScope.NameScope>
  729.     <NameScope />
  730. </NameScope.NameScope><NameScope.NameScope>
  731.     <NameScope />
  732. </NameScope.NameScope>   proto<NameScope.NameScope>
  733.     <NameScope />
  734. </NameScope.NameScope><NameScope.NameScope>
  735.     <NameScope />
  736. </NameScope.NameScope><NameScope.NameScope>
  737.     <NameScope />
  738. </NameScope.NameScope><NameScope.NameScope>
  739.     <NameScope />
  740. </NameScope.NameScope><NameScope.NameScope>
  741.     <NameScope />
  742. </NameScope.NameScope><NameScope.NameScope>
  743.     <NameScope />
  744. </NameScope.NameScope><NameScope.NameScope>
  745.     <NameScope />
  746. </NameScope.NameScope>Web/gRPC
  747. 接口<NameScope.NameScope>
  748.     <NameScope />
  749. </NameScope.NameScope><NameScope.NameScope>
  750.     <NameScope />
  751. </NameScope.NameScope><NameScope.NameScope>
  752.     <NameScope />
  753. </NameScope.NameScope><NameScope.NameScope>
  754.     <NameScope />
  755. </NameScope.NameScope><NameScope.NameScope>
  756.     <NameScope />
  757. </NameScope.NameScope><NameScope.NameScope>
  758.     <NameScope />
  759. </NameScope.NameScope><NameScope.NameScope>
  760.     <NameScope />
  761. </NameScope.NameScope><NameScope.NameScope>
  762.     <NameScope />
  763. </NameScope.NameScope><NameScope.NameScope>
  764.     <NameScope />
  765. </NameScope.NameScope> interface<NameScope.NameScope>
  766.     <NameScope />
  767. </NameScope.NameScope><NameScope.NameScope>
  768.     <NameScope />
  769. </NameScope.NameScope><NameScope.NameScope>
  770.     <NameScope />
  771. </NameScope.NameScope>[C#],VB<NameScope.NameScope>
  772.     <NameScope />
  773. </NameScope.NameScope> Common
  774. 控制台应用<NameScope.NameScope>
  775.     <NameScope />
  776. </NameScope.NameScope><NameScope.NameScope>
  777.     <NameScope />
  778. </NameScope.NameScope><NameScope.NameScope>
  779.     <NameScope />
  780. </NameScope.NameScope><NameScope.NameScope>
  781.     <NameScope />
  782. </NameScope.NameScope><NameScope.NameScope>
  783.     <NameScope />
  784. </NameScope.NameScope><NameScope.NameScope>
  785.     <NameScope />
  786. </NameScope.NameScope><NameScope.NameScope>
  787.     <NameScope />
  788. </NameScope.NameScope>   console<NameScope.NameScope>
  789.     <NameScope />
  790. </NameScope.NameScope><NameScope.NameScope>
  791.     <NameScope />
  792. </NameScope.NameScope><NameScope.NameScope>
  793.     <NameScope />
  794. </NameScope.NameScope>  [C#],F#,VB  Common/Console
  795. 枚举<NameScope.NameScope>
  796.     <NameScope />
  797. </NameScope.NameScope><NameScope.NameScope>
  798.     <NameScope />
  799. </NameScope.NameScope><NameScope.NameScope>
  800.     <NameScope />
  801. </NameScope.NameScope><NameScope.NameScope>
  802.     <NameScope />
  803. </NameScope.NameScope><NameScope.NameScope>
  804.     <NameScope />
  805. </NameScope.NameScope><NameScope.NameScope>
  806.     <NameScope />
  807. </NameScope.NameScope><NameScope.NameScope>
  808.     <NameScope />
  809. </NameScope.NameScope><NameScope.NameScope>
  810.     <NameScope />
  811. </NameScope.NameScope><NameScope.NameScope>
  812.     <NameScope />
  813. </NameScope.NameScope> enum<NameScope.NameScope>
  814.     <NameScope />
  815. </NameScope.NameScope><NameScope.NameScope>
  816.     <NameScope />
  817. </NameScope.NameScope><NameScope.NameScope>
  818.     <NameScope />
  819. </NameScope.NameScope><NameScope.NameScope>
  820.     <NameScope />
  821. </NameScope.NameScope> [C#],VB<NameScope.NameScope>
  822.     <NameScope />
  823. </NameScope.NameScope> Common
  824. 类<NameScope.NameScope>
  825.     <NameScope />
  826. </NameScope.NameScope><NameScope.NameScope>
  827.     <NameScope />
  828. </NameScope.NameScope><NameScope.NameScope>
  829.     <NameScope />
  830. </NameScope.NameScope><NameScope.NameScope>
  831.     <NameScope />
  832. </NameScope.NameScope><NameScope.NameScope>
  833.     <NameScope />
  834. </NameScope.NameScope><NameScope.NameScope>
  835.     <NameScope />
  836. </NameScope.NameScope><NameScope.NameScope>
  837.     <NameScope />
  838. </NameScope.NameScope><NameScope.NameScope>
  839.     <NameScope />
  840. </NameScope.NameScope><NameScope.NameScope>
  841.     <NameScope />
  842. </NameScope.NameScope>   class<NameScope.NameScope>
  843.     <NameScope />
  844. </NameScope.NameScope><NameScope.NameScope>
  845.     <NameScope />
  846. </NameScope.NameScope><NameScope.NameScope>
  847.     <NameScope />
  848. </NameScope.NameScope><NameScope.NameScope>
  849.     <NameScope />
  850. </NameScope.NameScope>[C#],VB<NameScope.NameScope>
  851.     <NameScope />
  852. </NameScope.NameScope> Common
  853. 类库<NameScope.NameScope>
  854.     <NameScope />
  855. </NameScope.NameScope><NameScope.NameScope>
  856.     <NameScope />
  857. </NameScope.NameScope><NameScope.NameScope>
  858.     <NameScope />
  859. </NameScope.NameScope><NameScope.NameScope>
  860.     <NameScope />
  861. </NameScope.NameScope><NameScope.NameScope>
  862.     <NameScope />
  863. </NameScope.NameScope><NameScope.NameScope>
  864.     <NameScope />
  865. </NameScope.NameScope><NameScope.NameScope>
  866.     <NameScope />
  867. </NameScope.NameScope><NameScope.NameScope>
  868.     <NameScope />
  869. </NameScope.NameScope><NameScope.NameScope>
  870.     <NameScope />
  871. </NameScope.NameScope> classlib<NameScope.NameScope>
  872.     <NameScope />
  873. </NameScope.NameScope><NameScope.NameScope>
  874.     <NameScope />
  875. </NameScope.NameScope><NameScope.NameScope>
  876.     <NameScope />
  877. </NameScope.NameScope> [C#],F#,VB  Common/Library
  878. 结构<NameScope.NameScope>
  879.     <NameScope />
  880. </NameScope.NameScope><NameScope.NameScope>
  881.     <NameScope />
  882. </NameScope.NameScope><NameScope.NameScope>
  883.     <NameScope />
  884. </NameScope.NameScope><NameScope.NameScope>
  885.     <NameScope />
  886. </NameScope.NameScope><NameScope.NameScope>
  887.     <NameScope />
  888. </NameScope.NameScope><NameScope.NameScope>
  889.     <NameScope />
  890. </NameScope.NameScope><NameScope.NameScope>
  891.     <NameScope />
  892. </NameScope.NameScope><NameScope.NameScope>
  893.     <NameScope />
  894. </NameScope.NameScope><NameScope.NameScope>
  895.     <NameScope />
  896. </NameScope.NameScope> struct,structure<NameScope.NameScope>
  897.     <NameScope />
  898. </NameScope.NameScope> [C#],VB<NameScope.NameScope>
  899.     <NameScope />
  900. </NameScope.NameScope> Common
  901. 解决方案文件<NameScope.NameScope>
  902.     <NameScope />
  903. </NameScope.NameScope><NameScope.NameScope>
  904.     <NameScope />
  905. </NameScope.NameScope><NameScope.NameScope>
  906.     <NameScope />
  907. </NameScope.NameScope><NameScope.NameScope>
  908.     <NameScope />
  909. </NameScope.NameScope><NameScope.NameScope>
  910.     <NameScope />
  911. </NameScope.NameScope><NameScope.NameScope>
  912.     <NameScope />
  913. </NameScope.NameScope><NameScope.NameScope>
  914.     <NameScope />
  915. </NameScope.NameScope> sln,solution<NameScope.NameScope>
  916.     <NameScope />
  917. </NameScope.NameScope><NameScope.NameScope>
  918.     <NameScope />
  919. </NameScope.NameScope><NameScope.NameScope>
  920.     <NameScope />
  921. </NameScope.NameScope><NameScope.NameScope>
  922.     <NameScope />
  923. </NameScope.NameScope><NameScope.NameScope>
  924.     <NameScope />
  925. </NameScope.NameScope> Solution
  926. 记录<NameScope.NameScope>
  927.     <NameScope />
  928. </NameScope.NameScope><NameScope.NameScope>
  929.     <NameScope />
  930. </NameScope.NameScope><NameScope.NameScope>
  931.     <NameScope />
  932. </NameScope.NameScope><NameScope.NameScope>
  933.     <NameScope />
  934. </NameScope.NameScope><NameScope.NameScope>
  935.     <NameScope />
  936. </NameScope.NameScope><NameScope.NameScope>
  937.     <NameScope />
  938. </NameScope.NameScope><NameScope.NameScope>
  939.     <NameScope />
  940. </NameScope.NameScope><NameScope.NameScope>
  941.     <NameScope />
  942. </NameScope.NameScope><NameScope.NameScope>
  943.     <NameScope />
  944. </NameScope.NameScope> record<NameScope.NameScope>
  945.     <NameScope />
  946. </NameScope.NameScope><NameScope.NameScope>
  947.     <NameScope />
  948. </NameScope.NameScope><NameScope.NameScope>
  949.     <NameScope />
  950. </NameScope.NameScope>   [C#]<NameScope.NameScope>
  951.     <NameScope />
  952. </NameScope.NameScope><NameScope.NameScope>
  953.     <NameScope />
  954. </NameScope.NameScope>Common
  955. 辅助角色服务<NameScope.NameScope>
  956.     <NameScope />
  957. </NameScope.NameScope><NameScope.NameScope>
  958.     <NameScope />
  959. </NameScope.NameScope><NameScope.NameScope>
  960.     <NameScope />
  961. </NameScope.NameScope><NameScope.NameScope>
  962.     <NameScope />
  963. </NameScope.NameScope><NameScope.NameScope>
  964.     <NameScope />
  965. </NameScope.NameScope><NameScope.NameScope>
  966.     <NameScope />
  967. </NameScope.NameScope><NameScope.NameScope>
  968.     <NameScope />
  969. </NameScope.NameScope> worker<NameScope.NameScope>
  970.     <NameScope />
  971. </NameScope.NameScope><NameScope.NameScope>
  972.     <NameScope />
  973. </NameScope.NameScope><NameScope.NameScope>
  974.     <NameScope />
  975. </NameScope.NameScope>   [C#],F#<NameScope.NameScope>
  976.     <NameScope />
  977. </NameScope.NameScope> Common/Worker/Web
复制代码
咱们平常用得多的都是前几那几个,比如 mvc、web、wpf、classlib 等。我们在命令中引用的就是项目模板的短名称即可。比如控制台就是 console。
-n 参数指定项目的名称,我这里是“MyApp”,-o 参数指定项目存放目录,“.” 表示当前目录。
接下来要改一下项目文件(*.csproj)。
  1. <Project Sdk="Microsoft.NET.Sdk">
  2.   <PropertyGroup>
  3. <NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope><OutputType>Exe</OutputType>
  6. <NameScope.NameScope>
  7.     <NameScope />
  8. </NameScope.NameScope><TargetFramework><em><strong>net7.0-windows</strong></em></TargetFramework>
  9. <NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope><ImplicitUsings>enable</ImplicitUsings>
  12. <NameScope.NameScope>
  13.     <NameScope />
  14. </NameScope.NameScope><Nullable>enable</Nullable>
  15. <NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope><UseWPF><strong><em>true</em></strong></UseWPF>
  18.   </PropertyGroup>
  19. </Project>
复制代码
1、添加 MSBuild 属性 UseWPF,且设置为 true。有了这个你才能在项目中引用 WPF 有关的程序集。同理,如果要使用 Windows Forms,就将 UseWindowsForms 属性设置为 true。
2、TargetFramework 要在.NET版本后加上“-windows”,表示这是 Windows 平台特定的,Linux 上不可用的。类似的如 net7.0-android 等。
至于 OutputType 属性要不要改为 WinExe,.NET 5 以上是不需要的,它会自动判断启不启动控制台窗口。
好了,保存,关闭项目文件。可以写代码了。
在写代码前,咱们先理清楚一些核心对象的关系。你才会知道怎么写。Application 类是 WPF 程序启动的核心对象,通常表示该应用程序相关的初始化。所以,在 Main 方法中记得 new 一个。你可别太聪明,千万不要直接从 Application.Current 静态属性来获取。因为这时应用程序还没初始化呢,Current 属性还是 null。Current 属性适合在项目的其他代码中方便访问 Application 对象而使用的。
如果你没别的东西初始化,那就调用 Application 对象的 Run 方法。应用程序正式启动,并且主线程会卡在(也不是真的卡)这里,直到程序要退出了才从 Run 方法返回。其间,调度器会不断调度/处理各线程上的消息,直到消息循环终止。
窗口应用程序当然要一个主窗口。表示窗口的基类是 Window,可以直接用它,也可以派生出自己的类,然后初始化要在窗口上显示的控件。
  1. public class MyWindow:Window
  2. {
  3. <NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>public MyWindow()
  6. <NameScope.NameScope>
  7.     <NameScope />
  8. </NameScope.NameScope>{
  9. <NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope><NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope>InitUI();
  14. <NameScope.NameScope>
  15.     <NameScope />
  16. </NameScope.NameScope>}
  17. <NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope>private void InitUI()
  20. <NameScope.NameScope>
  21.     <NameScope />
  22. </NameScope.NameScope>{
  23. <NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope><NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope>// 本窗口的属性
  28. <NameScope.NameScope>
  29.     <NameScope />
  30. </NameScope.NameScope><NameScope.NameScope>
  31.     <NameScope />
  32. </NameScope.NameScope>this.Title = "鼠爷快乐园";
  33. <NameScope.NameScope>
  34.     <NameScope />
  35. </NameScope.NameScope><NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope>this.Height = 225;
  38. <NameScope.NameScope>
  39.     <NameScope />
  40. </NameScope.NameScope><NameScope.NameScope>
  41.     <NameScope />
  42. </NameScope.NameScope>this.Width = 315;
  43. <NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope><NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope>// 启动时窗口在屏幕中央
  48. <NameScope.NameScope>
  49.     <NameScope />
  50. </NameScope.NameScope><NameScope.NameScope>
  51.     <NameScope />
  52. </NameScope.NameScope>WindowStartupLocation = WindowStartupLocation.CenterScreen;
  53. <NameScope.NameScope>
  54.     <NameScope />
  55. </NameScope.NameScope><NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope>// 整点控件
  58. <NameScope.NameScope>
  59.     <NameScope />
  60. </NameScope.NameScope><NameScope.NameScope>
  61.     <NameScope />
  62. </NameScope.NameScope>// 两个block
  63. <NameScope.NameScope>
  64.     <NameScope />
  65. </NameScope.NameScope><NameScope.NameScope>
  66.     <NameScope />
  67. </NameScope.NameScope>TextBlock tb1 = new()
  68. <NameScope.NameScope>
  69.     <NameScope />
  70. </NameScope.NameScope><NameScope.NameScope>
  71.     <NameScope />
  72. </NameScope.NameScope>{
  73. <NameScope.NameScope>
  74.     <NameScope />
  75. </NameScope.NameScope><NameScope.NameScope>
  76.     <NameScope />
  77. </NameScope.NameScope><NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope>Text = "每天毙一鼠",
  80. <NameScope.NameScope>
  81.     <NameScope />
  82. </NameScope.NameScope><NameScope.NameScope>
  83.     <NameScope />
  84. </NameScope.NameScope><NameScope.NameScope>
  85.     <NameScope />
  86. </NameScope.NameScope>TextAlignment = TextAlignment.Center,
  87. <NameScope.NameScope>
  88.     <NameScope />
  89. </NameScope.NameScope><NameScope.NameScope>
  90.     <NameScope />
  91. </NameScope.NameScope><NameScope.NameScope>
  92.     <NameScope />
  93. </NameScope.NameScope>// 文本颜色
  94. <NameScope.NameScope>
  95.     <NameScope />
  96. </NameScope.NameScope><NameScope.NameScope>
  97.     <NameScope />
  98. </NameScope.NameScope><NameScope.NameScope>
  99.     <NameScope />
  100. </NameScope.NameScope>Foreground = new SolidColorBrush(Color.FromRgb(12, 50, 208))
  101. <NameScope.NameScope>
  102.     <NameScope />
  103. </NameScope.NameScope><NameScope.NameScope>
  104.     <NameScope />
  105. </NameScope.NameScope>};
  106. <NameScope.NameScope>
  107.     <NameScope />
  108. </NameScope.NameScope><NameScope.NameScope>
  109.     <NameScope />
  110. </NameScope.NameScope>TextBlock tb2 = new()
  111. <NameScope.NameScope>
  112.     <NameScope />
  113. </NameScope.NameScope><NameScope.NameScope>
  114.     <NameScope />
  115. </NameScope.NameScope>{
  116. <NameScope.NameScope>
  117.     <NameScope />
  118. </NameScope.NameScope><NameScope.NameScope>
  119.     <NameScope />
  120. </NameScope.NameScope><NameScope.NameScope>
  121.     <NameScope />
  122. </NameScope.NameScope>Text = "添寿又增福",
  123. <NameScope.NameScope>
  124.     <NameScope />
  125. </NameScope.NameScope><NameScope.NameScope>
  126.     <NameScope />
  127. </NameScope.NameScope><NameScope.NameScope>
  128.     <NameScope />
  129. </NameScope.NameScope>TextAlignment = TextAlignment.Center
  130. <NameScope.NameScope>
  131.     <NameScope />
  132. </NameScope.NameScope><NameScope.NameScope>
  133.     <NameScope />
  134. </NameScope.NameScope>};
  135. <NameScope.NameScope>
  136.     <NameScope />
  137. </NameScope.NameScope><NameScope.NameScope>
  138.     <NameScope />
  139. </NameScope.NameScope>// 再加一个按钮
  140. <NameScope.NameScope>
  141.     <NameScope />
  142. </NameScope.NameScope><NameScope.NameScope>
  143.     <NameScope />
  144. </NameScope.NameScope>Button btn = new()
  145. <NameScope.NameScope>
  146.     <NameScope />
  147. </NameScope.NameScope><NameScope.NameScope>
  148.     <NameScope />
  149. </NameScope.NameScope>{
  150. <NameScope.NameScope>
  151.     <NameScope />
  152. </NameScope.NameScope><NameScope.NameScope>
  153.     <NameScope />
  154. </NameScope.NameScope><NameScope.NameScope>
  155.     <NameScope />
  156. </NameScope.NameScope>Content = "行动起来",
  157. <NameScope.NameScope>
  158.     <NameScope />
  159. </NameScope.NameScope><NameScope.NameScope>
  160.     <NameScope />
  161. </NameScope.NameScope><NameScope.NameScope>
  162.     <NameScope />
  163. </NameScope.NameScope>Margin = new Thickness(0d, 15d, 0d, 2d)
  164. <NameScope.NameScope>
  165.     <NameScope />
  166. </NameScope.NameScope><NameScope.NameScope>
  167.     <NameScope />
  168. </NameScope.NameScope>};
  169. <NameScope.NameScope>
  170.     <NameScope />
  171. </NameScope.NameScope><NameScope.NameScope>
  172.     <NameScope />
  173. </NameScope.NameScope>// 单击事件
  174. <NameScope.NameScope>
  175.     <NameScope />
  176. </NameScope.NameScope><NameScope.NameScope>
  177.     <NameScope />
  178. </NameScope.NameScope>btn.Click += OnClick;
  179. <NameScope.NameScope>
  180.     <NameScope />
  181. </NameScope.NameScope><NameScope.NameScope>
  182.     <NameScope />
  183. </NameScope.NameScope>// 布局控件
  184. <NameScope.NameScope>
  185.     <NameScope />
  186. </NameScope.NameScope><NameScope.NameScope>
  187.     <NameScope />
  188. </NameScope.NameScope>StackPanel panel = new();
  189. <NameScope.NameScope>
  190.     <NameScope />
  191. </NameScope.NameScope><NameScope.NameScope>
  192.     <NameScope />
  193. </NameScope.NameScope>// 垂直方向
  194. <NameScope.NameScope>
  195.     <NameScope />
  196. </NameScope.NameScope><NameScope.NameScope>
  197.     <NameScope />
  198. </NameScope.NameScope>panel.Orientation = Orientation.Vertical;
  199. <NameScope.NameScope>
  200.     <NameScope />
  201. </NameScope.NameScope><NameScope.NameScope>
  202.     <NameScope />
  203. </NameScope.NameScope>// 添加子元素
  204. <NameScope.NameScope>
  205.     <NameScope />
  206. </NameScope.NameScope><NameScope.NameScope>
  207.     <NameScope />
  208. </NameScope.NameScope>panel.Children.Add(tb1);
  209. <NameScope.NameScope>
  210.     <NameScope />
  211. </NameScope.NameScope><NameScope.NameScope>
  212.     <NameScope />
  213. </NameScope.NameScope>panel.Children.Add(tb2);
  214. <NameScope.NameScope>
  215.     <NameScope />
  216. </NameScope.NameScope><NameScope.NameScope>
  217.     <NameScope />
  218. </NameScope.NameScope>panel.Children.Add(btn);
  219. <NameScope.NameScope>
  220.     <NameScope />
  221. </NameScope.NameScope><NameScope.NameScope>
  222.     <NameScope />
  223. </NameScope.NameScope>// 作为窗口的内容
  224. <NameScope.NameScope>
  225.     <NameScope />
  226. </NameScope.NameScope><NameScope.NameScope>
  227.     <NameScope />
  228. </NameScope.NameScope>this.Content = panel;
  229. <NameScope.NameScope>
  230.     <NameScope />
  231. </NameScope.NameScope>}
  232. <NameScope.NameScope>
  233.     <NameScope />
  234. </NameScope.NameScope>private void OnClick(object sender, RoutedEventArgs e)
  235. <NameScope.NameScope>
  236.     <NameScope />
  237. </NameScope.NameScope>{
  238. <NameScope.NameScope>
  239.     <NameScope />
  240. </NameScope.NameScope><NameScope.NameScope>
  241.     <NameScope />
  242. </NameScope.NameScope>MessageBox.Show("自在其间乐");
  243. <NameScope.NameScope>
  244.     <NameScope />
  245. </NameScope.NameScope>}
  246. }
复制代码
Windows 属于内容控件,公开 Content 属性,用来设置单个对象引用。上述代码先创建两个 TextBlock 实例和一个 Button 实例,然后把它们塞进 StackPanel 中,再把 StackPanel 实例赋值给窗口的 Content 属性。
窗口类写好后,在 Main 方法中,调用 Run 方法时把窗口实例传进去。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>[STAThread]
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>static void Main(string[] args)
  7. <NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>{
  10. <NameScope.NameScope>
  11.     <NameScope />
  12. </NameScope.NameScope><NameScope.NameScope>
  13.     <NameScope />
  14. </NameScope.NameScope>Application app = new();
  15. <NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope><NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope>app.Run(new MyWindow());
  20. <NameScope.NameScope>
  21.     <NameScope />
  22. </NameScope.NameScope>}
复制代码
这个程序已经可以运行了。

 
************************************************************************************************
咱们继续探索。如果要用到数据绑定呢。在 XAML 中是用 {Binding} 扩展标记的,而在代码中对应的是 Binding 类,位于 System.Windows.Data 命名空间。
Binding 类的构造函数可以传递一个字符串常量,对应 {Binding Path=... } 中的 Path,即要绑定的对象路径。数据源则由 Source 属性设置。
  1. public Binding(string path);
复制代码
关联绑定用的是 BindingOperations 类的静态方法 SetBinding,要获取已关联的 Binding 对象就调用 GetBinding 方法。
  1. public static Binding GetBinding(DependencyObject target, DependencyProperty dp);
  2. public static BindingExpressionBase SetBinding(DependencyObject target, DependencyProperty dp, BindingBase binding);
复制代码
BindingOperations 类本身是静态类,所以它的成员自然也是静态的。target 参数是绑定目标,即 WPF 对象,dp 是要绑定的依赖属性,binding 就是Binding对象。
咱们举个例子。
假设用以下类作为数据源。
  1. public class Student
  2. {
  3. <NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>public int ID { get; set; }
  6. <NameScope.NameScope>
  7.     <NameScope />
  8. </NameScope.NameScope>public string? Name { get; set; }
  9. <NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope>public int Age { get; set; }
  12. }
复制代码
窗口的结构:内容根为 Grid 对象,它包含三行两列,用来放六个 TextBlock 控件。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope><NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>Grid layout = new();
  6. <NameScope.NameScope>
  7.     <NameScope />
  8. </NameScope.NameScope><NameScope.NameScope>
  9.     <NameScope />
  10. </NameScope.NameScope>// 设置为窗口内容
  11. <NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope><NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope>this.Content = layout;
  16. <NameScope.NameScope>
  17.     <NameScope />
  18. </NameScope.NameScope><NameScope.NameScope>
  19.     <NameScope />
  20. </NameScope.NameScope>// 设置边距
  21. <NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope><NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope>layout.Margin = new Thickness(13.5d);
  26. <NameScope.NameScope>
  27.     <NameScope />
  28. </NameScope.NameScope><NameScope.NameScope>
  29.     <NameScope />
  30. </NameScope.NameScope>// 三行两列
  31. <NameScope.NameScope>
  32.     <NameScope />
  33. </NameScope.NameScope><NameScope.NameScope>
  34.     <NameScope />
  35. </NameScope.NameScope>layout.ColumnDefinitions.Add(new ColumnDefinition()
  36. <NameScope.NameScope>
  37.     <NameScope />
  38. </NameScope.NameScope><NameScope.NameScope>
  39.     <NameScope />
  40. </NameScope.NameScope>{
  41. <NameScope.NameScope>
  42.     <NameScope />
  43. </NameScope.NameScope><NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope><NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope>Width = GridLength.Auto
  48. <NameScope.NameScope>
  49.     <NameScope />
  50. </NameScope.NameScope><NameScope.NameScope>
  51.     <NameScope />
  52. </NameScope.NameScope>});
  53. <NameScope.NameScope>
  54.     <NameScope />
  55. </NameScope.NameScope><NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope>layout.ColumnDefinitions.Add(new ColumnDefinition(){
  58. <NameScope.NameScope>
  59.     <NameScope />
  60. </NameScope.NameScope><NameScope.NameScope>
  61.     <NameScope />
  62. </NameScope.NameScope><NameScope.NameScope>
  63.     <NameScope />
  64. </NameScope.NameScope>// 星号,即 1*
  65. <NameScope.NameScope>
  66.     <NameScope />
  67. </NameScope.NameScope><NameScope.NameScope>
  68.     <NameScope />
  69. </NameScope.NameScope><NameScope.NameScope>
  70.     <NameScope />
  71. </NameScope.NameScope>Width = new GridLength(1.0d, GridUnitType.Star)
  72. <NameScope.NameScope>
  73.     <NameScope />
  74. </NameScope.NameScope><NameScope.NameScope>
  75.     <NameScope />
  76. </NameScope.NameScope>});
  77. <NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope><NameScope.NameScope>
  80.     <NameScope />
  81. </NameScope.NameScope>layout.RowDefinitions.Add(new()
  82. <NameScope.NameScope>
  83.     <NameScope />
  84. </NameScope.NameScope><NameScope.NameScope>
  85.     <NameScope />
  86. </NameScope.NameScope>{
  87. <NameScope.NameScope>
  88.     <NameScope />
  89. </NameScope.NameScope><NameScope.NameScope>
  90.     <NameScope />
  91. </NameScope.NameScope><NameScope.NameScope>
  92.     <NameScope />
  93. </NameScope.NameScope>Height = GridLength.Auto
  94. <NameScope.NameScope>
  95.     <NameScope />
  96. </NameScope.NameScope><NameScope.NameScope>
  97.     <NameScope />
  98. </NameScope.NameScope>});
  99. <NameScope.NameScope>
  100.     <NameScope />
  101. </NameScope.NameScope><NameScope.NameScope>
  102.     <NameScope />
  103. </NameScope.NameScope>layout.RowDefinitions.Add(new()
  104. <NameScope.NameScope>
  105.     <NameScope />
  106. </NameScope.NameScope><NameScope.NameScope>
  107.     <NameScope />
  108. </NameScope.NameScope>{
  109. <NameScope.NameScope>
  110.     <NameScope />
  111. </NameScope.NameScope><NameScope.NameScope>
  112.     <NameScope />
  113. </NameScope.NameScope><NameScope.NameScope>
  114.     <NameScope />
  115. </NameScope.NameScope>Height = GridLength.Auto
  116. <NameScope.NameScope>
  117.     <NameScope />
  118. </NameScope.NameScope><NameScope.NameScope>
  119.     <NameScope />
  120. </NameScope.NameScope>});
  121. <NameScope.NameScope>
  122.     <NameScope />
  123. </NameScope.NameScope><NameScope.NameScope>
  124.     <NameScope />
  125. </NameScope.NameScope>layout.RowDefinitions.Add(new()
  126. <NameScope.NameScope>
  127.     <NameScope />
  128. </NameScope.NameScope><NameScope.NameScope>
  129.     <NameScope />
  130. </NameScope.NameScope>{
  131. <NameScope.NameScope>
  132.     <NameScope />
  133. </NameScope.NameScope><NameScope.NameScope>
  134.     <NameScope />
  135. </NameScope.NameScope><NameScope.NameScope>
  136.     <NameScope />
  137. </NameScope.NameScope>Height = GridLength.Auto
  138. <NameScope.NameScope>
  139.     <NameScope />
  140. </NameScope.NameScope><NameScope.NameScope>
  141.     <NameScope />
  142. </NameScope.NameScope>});
复制代码
ColumnDefinitions 用来定义列。上述代码中,第一列的宽度为 Auto,第二列的宽度为 *。
RowDefinitions 集合用来定义行。上述代码中,三行的高度都是 Auto。
然后 new 出六个 TextBlock,三个用来显示字段标签,三个用于数据绑定,显示属性值。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope><NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>// 六个block
  6. <NameScope.NameScope>
  7.     <NameScope />
  8. </NameScope.NameScope><NameScope.NameScope>
  9.     <NameScope />
  10. </NameScope.NameScope>var tbIDf = new TextBlock(){
  11. <NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope><NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope><NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>Text = "学号:"
  18. <NameScope.NameScope>
  19.     <NameScope />
  20. </NameScope.NameScope><NameScope.NameScope>
  21.     <NameScope />
  22. </NameScope.NameScope>};
  23. <NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope><NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope>var tbNamef = new TextBlock()
  28. <NameScope.NameScope>
  29.     <NameScope />
  30. </NameScope.NameScope><NameScope.NameScope>
  31.     <NameScope />
  32. </NameScope.NameScope>{
  33. <NameScope.NameScope>
  34.     <NameScope />
  35. </NameScope.NameScope><NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope><NameScope.NameScope>
  38.     <NameScope />
  39. </NameScope.NameScope>Text = "姓名:"
  40. <NameScope.NameScope>
  41.     <NameScope />
  42. </NameScope.NameScope><NameScope.NameScope>
  43.     <NameScope />
  44. </NameScope.NameScope>};
  45. <NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope><NameScope.NameScope>
  48.     <NameScope />
  49. </NameScope.NameScope>var tbAgef = new TextBlock()
  50. <NameScope.NameScope>
  51.     <NameScope />
  52. </NameScope.NameScope><NameScope.NameScope>
  53.     <NameScope />
  54. </NameScope.NameScope>{
  55. <NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope><NameScope.NameScope>
  58.     <NameScope />
  59. </NameScope.NameScope><NameScope.NameScope>
  60.     <NameScope />
  61. </NameScope.NameScope>Text = "年龄:"
  62. <NameScope.NameScope>
  63.     <NameScope />
  64. </NameScope.NameScope><NameScope.NameScope>
  65.     <NameScope />
  66. </NameScope.NameScope>};
  67. <NameScope.NameScope>
  68.     <NameScope />
  69. </NameScope.NameScope><NameScope.NameScope>
  70.     <NameScope />
  71. </NameScope.NameScope>TextBlock tbIDv = new();
  72. <NameScope.NameScope>
  73.     <NameScope />
  74. </NameScope.NameScope><NameScope.NameScope>
  75.     <NameScope />
  76. </NameScope.NameScope>var tbNamev = new TextBlock();
  77. <NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope><NameScope.NameScope>
  80.     <NameScope />
  81. </NameScope.NameScope>var tbAgev = new TextBlock();
复制代码
把六个 TextBlock 控件添加到 Grid 的子级中。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope><NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>layout.Children.Add(tbIDf);
  6. <NameScope.NameScope>
  7.     <NameScope />
  8. </NameScope.NameScope><NameScope.NameScope>
  9.     <NameScope />
  10. </NameScope.NameScope>layout.Children.Add(tbIDv);
  11. <NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope><NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope>layout.Children.Add(tbNamef);
  16. <NameScope.NameScope>
  17.     <NameScope />
  18. </NameScope.NameScope><NameScope.NameScope>
  19.     <NameScope />
  20. </NameScope.NameScope>layout.Children.Add(tbNamev);
  21. <NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope><NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope>layout.Children.Add(tbAgef);
  26. <NameScope.NameScope>
  27.     <NameScope />
  28. </NameScope.NameScope><NameScope.NameScope>
  29.     <NameScope />
  30. </NameScope.NameScope>layout.Children.Add(tbAgev);
复制代码
设置子元素所在行、列的 Grid.Row 和 Grid.Column 是附加属性,以 Grid.SetXXX 方法调用。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>layout.Children.Add(tbIDf);
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>layout.Children.Add(tbIDv);
  7. <NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>layout.Children.Add(tbNamef);
  10. <NameScope.NameScope>
  11.     <NameScope />
  12. </NameScope.NameScope>layout.Children.Add(tbNamev);
  13. <NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope>layout.Children.Add(tbAgef);
  16. <NameScope.NameScope>
  17.     <NameScope />
  18. </NameScope.NameScope>layout.Children.Add(tbAgev);
  19. <NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope>// Row、Column 附加属性
  22. <NameScope.NameScope>
  23.     <NameScope />
  24. </NameScope.NameScope>// 第一行第一列
  25. <NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope>Grid.SetRow(tbIDf, 0);
  28. <NameScope.NameScope>
  29.     <NameScope />
  30. </NameScope.NameScope>Grid.SetColumn(tbIDf, 0);
  31. <NameScope.NameScope>
  32.     <NameScope />
  33. </NameScope.NameScope>// 第一行第二列
  34. <NameScope.NameScope>
  35.     <NameScope />
  36. </NameScope.NameScope>Grid.SetRow(tbIDv, 0);
  37. <NameScope.NameScope>
  38.     <NameScope />
  39. </NameScope.NameScope>Grid.SetColumn(tbIDv, 1);
  40. <NameScope.NameScope>
  41.     <NameScope />
  42. </NameScope.NameScope>// 第二行第一列
  43. <NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope>Grid.SetRow(tbNamef, 1);
  46. <NameScope.NameScope>
  47.     <NameScope />
  48. </NameScope.NameScope>Grid.SetColumn(tbNamef, 0);
  49. <NameScope.NameScope>
  50.     <NameScope />
  51. </NameScope.NameScope>// 第二行第二列
  52. <NameScope.NameScope>
  53.     <NameScope />
  54. </NameScope.NameScope>Grid.SetRow(tbNamev, 1);
  55. <NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope>Grid.SetColumn(tbNamev, 1);
  58. <NameScope.NameScope>
  59.     <NameScope />
  60. </NameScope.NameScope>// 第三行第一列
  61. <NameScope.NameScope>
  62.     <NameScope />
  63. </NameScope.NameScope>Grid.SetRow(tbAgef, 2);
  64. <NameScope.NameScope>
  65.     <NameScope />
  66. </NameScope.NameScope>Grid.SetColumn(tbAgef,0);
  67. <NameScope.NameScope>
  68.     <NameScope />
  69. </NameScope.NameScope>// 第三行第二列
  70. <NameScope.NameScope>
  71.     <NameScope />
  72. </NameScope.NameScope>Grid.SetRow(tbAgev, 2);
  73. <NameScope.NameScope>
  74.     <NameScope />
  75. </NameScope.NameScope>Grid.SetColumn(tbAgev, 1);
复制代码
最后是创建三个 Binding ,为 Student 类的三个属性做绑定。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>/* ID */
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>Binding bindID = new(nameof(Student.ID))
  7. <NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>{
  10. <NameScope.NameScope>
  11.     <NameScope />
  12. </NameScope.NameScope><NameScope.NameScope>
  13.     <NameScope />
  14. </NameScope.NameScope>Source = this.stu
  15. <NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>};
  18. <NameScope.NameScope>
  19.     <NameScope />
  20. </NameScope.NameScope>BindingOperations.SetBinding(tbIDv, TextBlock.TextProperty, bindID);
  21. <NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope>/* Name */
  24. <NameScope.NameScope>
  25.     <NameScope />
  26. </NameScope.NameScope>Binding bindName = new(nameof(Student.Name))
  27. <NameScope.NameScope>
  28.     <NameScope />
  29. </NameScope.NameScope>{
  30. <NameScope.NameScope>
  31.     <NameScope />
  32. </NameScope.NameScope><NameScope.NameScope>
  33.     <NameScope />
  34. </NameScope.NameScope>Source = stu
  35. <NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope>};
  38. <NameScope.NameScope>
  39.     <NameScope />
  40. </NameScope.NameScope>BindingOperations.SetBinding(tbNamev, TextBlock.TextProperty,
  41. bindName);
  42. <NameScope.NameScope>
  43.     <NameScope />
  44. </NameScope.NameScope>/* Age */
  45. <NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope>Binding bindAge = new(nameof(Student.Age))
  48. <NameScope.NameScope>
  49.     <NameScope />
  50. </NameScope.NameScope>{
  51. <NameScope.NameScope>
  52.     <NameScope />
  53. </NameScope.NameScope><NameScope.NameScope>
  54.     <NameScope />
  55. </NameScope.NameScope>Source = stu
  56. <NameScope.NameScope>
  57.     <NameScope />
  58. </NameScope.NameScope>};
  59. <NameScope.NameScope>
  60.     <NameScope />
  61. </NameScope.NameScope>BindingOperations.SetBinding(tbAgev, TextBlock.TextProperty, bindAge);
复制代码
完整的初始化方法代码如下:
  1. private void InitUI(){<NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>Title = "数据绑定";<NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>Width = 240;<NameScope.NameScope>
  6.     <NameScope />
  7. </NameScope.NameScope>Height = 185;<NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>// 创建Grid<NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope>Grid layout = new();<NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope>// 设置为窗口内容<NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope>this.Content = layout;<NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>// 设置边距<NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope>layout.Margin = new Thickness(13.5d);<NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope>// 三行两列<NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope>layout.ColumnDefinitions.Add(new ColumnDefinition()<NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope>{<NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope><NameScope.NameScope>
  28.     <NameScope />
  29. </NameScope.NameScope>Width = GridLength.Auto<NameScope.NameScope>
  30.     <NameScope />
  31. </NameScope.NameScope>});<NameScope.NameScope>
  32.     <NameScope />
  33. </NameScope.NameScope>layout.ColumnDefinitions.Add(new ColumnDefinition(){<NameScope.NameScope>
  34.     <NameScope />
  35. </NameScope.NameScope><NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope>// 星号,即 1*<NameScope.NameScope>
  38.     <NameScope />
  39. </NameScope.NameScope><NameScope.NameScope>
  40.     <NameScope />
  41. </NameScope.NameScope>Width = new GridLength(1.0d, GridUnitType.Star)<NameScope.NameScope>
  42.     <NameScope />
  43. </NameScope.NameScope>});<NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope>layout.RowDefinitions.Add(new()<NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope>{<NameScope.NameScope>
  48.     <NameScope />
  49. </NameScope.NameScope><NameScope.NameScope>
  50.     <NameScope />
  51. </NameScope.NameScope>Height = GridLength.Auto<NameScope.NameScope>
  52.     <NameScope />
  53. </NameScope.NameScope>});<NameScope.NameScope>
  54.     <NameScope />
  55. </NameScope.NameScope>layout.RowDefinitions.Add(new()<NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope>{<NameScope.NameScope>
  58.     <NameScope />
  59. </NameScope.NameScope><NameScope.NameScope>
  60.     <NameScope />
  61. </NameScope.NameScope>Height = GridLength.Auto<NameScope.NameScope>
  62.     <NameScope />
  63. </NameScope.NameScope>});<NameScope.NameScope>
  64.     <NameScope />
  65. </NameScope.NameScope>layout.RowDefinitions.Add(new()<NameScope.NameScope>
  66.     <NameScope />
  67. </NameScope.NameScope>{<NameScope.NameScope>
  68.     <NameScope />
  69. </NameScope.NameScope><NameScope.NameScope>
  70.     <NameScope />
  71. </NameScope.NameScope>Height = GridLength.Auto<NameScope.NameScope>
  72.     <NameScope />
  73. </NameScope.NameScope>});<NameScope.NameScope>
  74.     <NameScope />
  75. </NameScope.NameScope><NameScope.NameScope>
  76.     <NameScope />
  77. </NameScope.NameScope>// 六个block<NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope>var tbIDf = new TextBlock(){<NameScope.NameScope>
  80.     <NameScope />
  81. </NameScope.NameScope><NameScope.NameScope>
  82.     <NameScope />
  83. </NameScope.NameScope>Text = "学号:"<NameScope.NameScope>
  84.     <NameScope />
  85. </NameScope.NameScope>};<NameScope.NameScope>
  86.     <NameScope />
  87. </NameScope.NameScope>var tbNamef = new TextBlock()<NameScope.NameScope>
  88.     <NameScope />
  89. </NameScope.NameScope>{<NameScope.NameScope>
  90.     <NameScope />
  91. </NameScope.NameScope><NameScope.NameScope>
  92.     <NameScope />
  93. </NameScope.NameScope>Text = "姓名:"<NameScope.NameScope>
  94.     <NameScope />
  95. </NameScope.NameScope>};<NameScope.NameScope>
  96.     <NameScope />
  97. </NameScope.NameScope>var tbAgef = new TextBlock()<NameScope.NameScope>
  98.     <NameScope />
  99. </NameScope.NameScope>{<NameScope.NameScope>
  100.     <NameScope />
  101. </NameScope.NameScope><NameScope.NameScope>
  102.     <NameScope />
  103. </NameScope.NameScope>Text = "年龄:"<NameScope.NameScope>
  104.     <NameScope />
  105. </NameScope.NameScope>};<NameScope.NameScope>
  106.     <NameScope />
  107. </NameScope.NameScope>TextBlock tbIDv = new();<NameScope.NameScope>
  108.     <NameScope />
  109. </NameScope.NameScope>var tbNamev = new TextBlock();<NameScope.NameScope>
  110.     <NameScope />
  111. </NameScope.NameScope>var tbAgev = new TextBlock();<NameScope.NameScope>
  112.     <NameScope />
  113. </NameScope.NameScope>// 把六个block放到 grid 上<NameScope.NameScope>
  114.     <NameScope />
  115. </NameScope.NameScope>layout.Children.Add(tbIDf);
  116. <NameScope.NameScope>
  117.     <NameScope />
  118. </NameScope.NameScope>layout.Children.Add(tbIDv);
  119. <NameScope.NameScope>
  120.     <NameScope />
  121. </NameScope.NameScope>layout.Children.Add(tbNamef);
  122. <NameScope.NameScope>
  123.     <NameScope />
  124. </NameScope.NameScope>layout.Children.Add(tbNamev);
  125. <NameScope.NameScope>
  126.     <NameScope />
  127. </NameScope.NameScope>layout.Children.Add(tbAgef);
  128. <NameScope.NameScope>
  129.     <NameScope />
  130. </NameScope.NameScope>layout.Children.Add(tbAgev);
  131. <NameScope.NameScope>
  132.     <NameScope />
  133. </NameScope.NameScope>// Row、Column 附加属性
  134. <NameScope.NameScope>
  135.     <NameScope />
  136. </NameScope.NameScope>// 第一行第一列
  137. <NameScope.NameScope>
  138.     <NameScope />
  139. </NameScope.NameScope>Grid.SetRow(tbIDf, 0);
  140. <NameScope.NameScope>
  141.     <NameScope />
  142. </NameScope.NameScope>Grid.SetColumn(tbIDf, 0);
  143. <NameScope.NameScope>
  144.     <NameScope />
  145. </NameScope.NameScope>// 第一行第二列
  146. <NameScope.NameScope>
  147.     <NameScope />
  148. </NameScope.NameScope>Grid.SetRow(tbIDv, 0);
  149. <NameScope.NameScope>
  150.     <NameScope />
  151. </NameScope.NameScope>Grid.SetColumn(tbIDv, 1);
  152. <NameScope.NameScope>
  153.     <NameScope />
  154. </NameScope.NameScope>// 第二行第一列
  155. <NameScope.NameScope>
  156.     <NameScope />
  157. </NameScope.NameScope>Grid.SetRow(tbNamef, 1);
  158. <NameScope.NameScope>
  159.     <NameScope />
  160. </NameScope.NameScope>Grid.SetColumn(tbNamef, 0);
  161. <NameScope.NameScope>
  162.     <NameScope />
  163. </NameScope.NameScope>// 第二行第二列
  164. <NameScope.NameScope>
  165.     <NameScope />
  166. </NameScope.NameScope>Grid.SetRow(tbNamev, 1);
  167. <NameScope.NameScope>
  168.     <NameScope />
  169. </NameScope.NameScope>Grid.SetColumn(tbNamev, 1);
  170. <NameScope.NameScope>
  171.     <NameScope />
  172. </NameScope.NameScope>// 第三行第一列
  173. <NameScope.NameScope>
  174.     <NameScope />
  175. </NameScope.NameScope>Grid.SetRow(tbAgef, 2);
  176. <NameScope.NameScope>
  177.     <NameScope />
  178. </NameScope.NameScope>Grid.SetColumn(tbAgef,0);
  179. <NameScope.NameScope>
  180.     <NameScope />
  181. </NameScope.NameScope>// 第三行第二列
  182. <NameScope.NameScope>
  183.     <NameScope />
  184. </NameScope.NameScope>Grid.SetRow(tbAgev, 2);
  185. <NameScope.NameScope>
  186.     <NameScope />
  187. </NameScope.NameScope>Grid.SetColumn(tbAgev, 1);<NameScope.NameScope>
  188.     <NameScope />
  189. </NameScope.NameScope>// 数据绑定<NameScope.NameScope>
  190.     <NameScope />
  191. </NameScope.NameScope>/* ID */
  192. <NameScope.NameScope>
  193.     <NameScope />
  194. </NameScope.NameScope>Binding bindID = new(nameof(Student.ID))
  195. <NameScope.NameScope>
  196.     <NameScope />
  197. </NameScope.NameScope>{
  198. <NameScope.NameScope>
  199.     <NameScope />
  200. </NameScope.NameScope><NameScope.NameScope>
  201.     <NameScope />
  202. </NameScope.NameScope>Source = this.stu
  203. <NameScope.NameScope>
  204.     <NameScope />
  205. </NameScope.NameScope>};
  206. <NameScope.NameScope>
  207.     <NameScope />
  208. </NameScope.NameScope>BindingOperations.SetBinding(tbIDv, TextBlock.TextProperty, bindID);
  209. <NameScope.NameScope>
  210.     <NameScope />
  211. </NameScope.NameScope>/* Name */
  212. <NameScope.NameScope>
  213.     <NameScope />
  214. </NameScope.NameScope>Binding bindName = new(nameof(Student.Name))
  215. <NameScope.NameScope>
  216.     <NameScope />
  217. </NameScope.NameScope>{
  218. <NameScope.NameScope>
  219.     <NameScope />
  220. </NameScope.NameScope><NameScope.NameScope>
  221.     <NameScope />
  222. </NameScope.NameScope>Source = stu
  223. <NameScope.NameScope>
  224.     <NameScope />
  225. </NameScope.NameScope>};
  226. <NameScope.NameScope>
  227.     <NameScope />
  228. </NameScope.NameScope>BindingOperations.SetBinding(tbNamev, TextBlock.TextProperty,
  229. bindName);
  230. <NameScope.NameScope>
  231.     <NameScope />
  232. </NameScope.NameScope>/* Age */
  233. <NameScope.NameScope>
  234.     <NameScope />
  235. </NameScope.NameScope>Binding bindAge = new(nameof(Student.Age))
  236. <NameScope.NameScope>
  237.     <NameScope />
  238. </NameScope.NameScope>{
  239. <NameScope.NameScope>
  240.     <NameScope />
  241. </NameScope.NameScope><NameScope.NameScope>
  242.     <NameScope />
  243. </NameScope.NameScope>Source = stu
  244. <NameScope.NameScope>
  245.     <NameScope />
  246. </NameScope.NameScope>};
  247. <NameScope.NameScope>
  248.     <NameScope />
  249. </NameScope.NameScope>BindingOperations.SetBinding(tbAgev, TextBlock.TextProperty, bindAge);}
复制代码
运行效果如下:

 
这时候,估计你也想到了一件事—— WPF 元素之间的绑定咋弄?对应的 XAML 扩展标记 {Binding ElementName=..., Path=... }。这个用代码写起来也不难,Binding 类有 ElementName 属性,可以引用已命名的对象。但是,在代码里面,咱们是直接用变量名来引用对象的,并没有分配对象名称。虽然 FrameworkElement 类的子类都继承了 Name 属性,但,设置这个 Name 属性 Binding.ElementName 是找不到的,必须要在 NameScope 对象里注册到XAML名称空间后才能被 ElementName 引用。
NameScope 类其实是个 Key=String, Value = Object 的字典,维护当前名称空间范围内的对象列表。对应的是 XAML 中的 x:Name = ...。NameScope 类定义了 NameScope 附加属性,允许将 NameScope 实例设置到目标对象上。XAML 语法是
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>
复制代码
但我们在写 XAML 时是不需要,都是自动添加的,用 x:Name 就行了。
在代码中用 SetNameScope 方法设置。
 
看看下面的例子。
  1. void InitUI(){<NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>Title = "元素之间绑定";<NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>// 根据内容自动调整窗口大小<NameScope.NameScope>
  6.     <NameScope />
  7. </NameScope.NameScope>SizeToContent = SizeToContent.WidthAndHeight;<NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>StackPanel panel = new(){<NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope><NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope>Orientation = Orientation.Vertical,<NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope><NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>Margin = new Thickness(15.0d)<NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope>};<NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope>this.Content = panel;   // 布局<NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope>// 文本输入控件<NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope>TextBox txt = new TextBox();<NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope>txt.Margin = new Thickness(3.0d, 5.0d, 3.0d, 8.5d);<NameScope.NameScope>
  28.     <NameScope />
  29. </NameScope.NameScope>// 给它分配一个名字,绑定时用到<NameScope.NameScope>
  30.     <NameScope />
  31. </NameScope.NameScope>NameScope myScope = new();<NameScope.NameScope>
  32.     <NameScope />
  33. </NameScope.NameScope>NameScope.SetNameScope(this, myScope);<NameScope.NameScope>
  34.     <NameScope />
  35. </NameScope.NameScope>myScope.RegisterName("txtInput", txt);<NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope>// 文本块<NameScope.NameScope>
  38.     <NameScope />
  39. </NameScope.NameScope>TextBlock tb = new TextBlock();<NameScope.NameScope>
  40.     <NameScope />
  41. </NameScope.NameScope>tb.Margin = new Thickness(5.0d, 0d, 5.0d, 0d);<NameScope.NameScope>
  42.     <NameScope />
  43. </NameScope.NameScope>// 绑定<NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope>Binding bind = new();<NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope>bind.ElementName = "txtInput";<NameScope.NameScope>
  48.     <NameScope />
  49. </NameScope.NameScope>bind.Path = new PropertyPath(TextBox.TextProperty);<NameScope.NameScope>
  50.     <NameScope />
  51. </NameScope.NameScope>bind.Mode = BindingMode.OneWay;<NameScope.NameScope>
  52.     <NameScope />
  53. </NameScope.NameScope>// 在文本框更改时更新数据<NameScope.NameScope>
  54.     <NameScope />
  55. </NameScope.NameScope>bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;<NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope>BindingOperations.SetBinding(tb, TextBlock.TextProperty, bind);<NameScope.NameScope>
  58.     <NameScope />
  59. </NameScope.NameScope>// 添加到布局<NameScope.NameScope>
  60.     <NameScope />
  61. </NameScope.NameScope>panel.Children.Add(txt);<NameScope.NameScope>
  62.     <NameScope />
  63. </NameScope.NameScope>panel.Children.Add(tb);}
复制代码
这个方法也是写在 Window 的派生类中,SizeToContent = SizeToContent.WidthAndHeight 表示本窗口的宽度和高度会根据它要显示的内容自动调整。
由于 TextBlock 控件的文本来源于 TextBox,因此,要为 TextBox 注册一个名字“txtInput”。由于 FrameworkElement 类有 RegisterName 方法,所以,注册名称的代码也可以这样写:
  1. NameScope.SetNameScope(this, new NameScope());
  2. this.RegisterName("txtInput", txt);
复制代码
设置 bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 允许在输入的内容更改后就马上更新绑定数据,能做到实时显示输入的内容。
效果如下:

好了,今天咱们先说到这儿,剩下的如模板、样式、动画、3D 什么的,咱们下次再探讨。
 

来源:https://www.cnblogs.com/tcjiaan/archive/2023/06/15/17482371.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具