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

打印 上一主题 下一主题

主题 723|帖子 723|积分 2169

学习 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>  标记
  18. ---------------------------------------  -------------------  ----------  --------------------------------
  19. ASP.NET Core gRPC 服务<NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope><NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope><NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope> grpc<NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope><NameScope.NameScope>
  28.     <NameScope />
  29. </NameScope.NameScope>     [C#]<NameScope.NameScope>
  30.     <NameScope />
  31. </NameScope.NameScope>  Web/gRPC
  32. ASP.NET Core Web API<NameScope.NameScope>
  33.     <NameScope />
  34. </NameScope.NameScope><NameScope.NameScope>
  35.     <NameScope />
  36. </NameScope.NameScope><NameScope.NameScope>
  37.     <NameScope />
  38. </NameScope.NameScope>   webapi<NameScope.NameScope>
  39.     <NameScope />
  40. </NameScope.NameScope><NameScope.NameScope>
  41.     <NameScope />
  42. </NameScope.NameScope>   [C#],F#     Web/WebAPI
  43. ASP.NET Core Web 应用<NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope><NameScope.NameScope>
  46.     <NameScope />
  47. </NameScope.NameScope><NameScope.NameScope>
  48.     <NameScope />
  49. </NameScope.NameScope>  webapp,razor<NameScope.NameScope>
  50.     <NameScope />
  51. </NameScope.NameScope>   [C#]<NameScope.NameScope>
  52.     <NameScope />
  53. </NameScope.NameScope>  Web/MVC/Razor Pages
  54. ASP.NET Core Web 应用(模型-视图-控制器)  mvc<NameScope.NameScope>
  55.     <NameScope />
  56. </NameScope.NameScope><NameScope.NameScope>
  57.     <NameScope />
  58. </NameScope.NameScope><NameScope.NameScope>
  59.     <NameScope />
  60. </NameScope.NameScope>[C#],F#     Web/MVC
  61. ASP.NET Core 与 Angular<NameScope.NameScope>
  62.     <NameScope />
  63. </NameScope.NameScope><NameScope.NameScope>
  64.     <NameScope />
  65. </NameScope.NameScope><NameScope.NameScope>
  66.     <NameScope />
  67. </NameScope.NameScope>angular<NameScope.NameScope>
  68.     <NameScope />
  69. </NameScope.NameScope><NameScope.NameScope>
  70.     <NameScope />
  71. </NameScope.NameScope>  [C#]<NameScope.NameScope>
  72.     <NameScope />
  73. </NameScope.NameScope>  Web/MVC/SPA
  74. ASP.NET Core 与 React.js<NameScope.NameScope>
  75.     <NameScope />
  76. </NameScope.NameScope><NameScope.NameScope>
  77.     <NameScope />
  78. </NameScope.NameScope>     react<NameScope.NameScope>
  79.     <NameScope />
  80. </NameScope.NameScope><NameScope.NameScope>
  81.     <NameScope />
  82. </NameScope.NameScope>    [C#]<NameScope.NameScope>
  83.     <NameScope />
  84. </NameScope.NameScope>  Web/MVC/SPA
  85. ASP.NET Core 空<NameScope.NameScope>
  86.     <NameScope />
  87. </NameScope.NameScope><NameScope.NameScope>
  88.     <NameScope />
  89. </NameScope.NameScope><NameScope.NameScope>
  90.     <NameScope />
  91. </NameScope.NameScope><NameScope.NameScope>
  92.     <NameScope />
  93. </NameScope.NameScope>  web<NameScope.NameScope>
  94.     <NameScope />
  95. </NameScope.NameScope><NameScope.NameScope>
  96.     <NameScope />
  97. </NameScope.NameScope><NameScope.NameScope>
  98.     <NameScope />
  99. </NameScope.NameScope>[C#],F#     Web/Empty
  100. Blazor Server 应用<NameScope.NameScope>
  101.     <NameScope />
  102. </NameScope.NameScope><NameScope.NameScope>
  103.     <NameScope />
  104. </NameScope.NameScope><NameScope.NameScope>
  105.     <NameScope />
  106. </NameScope.NameScope>     blazorserver<NameScope.NameScope>
  107.     <NameScope />
  108. </NameScope.NameScope>   [C#]<NameScope.NameScope>
  109.     <NameScope />
  110. </NameScope.NameScope>  Web/Blazor
  111. Blazor Server 应用空<NameScope.NameScope>
  112.     <NameScope />
  113. </NameScope.NameScope><NameScope.NameScope>
  114.     <NameScope />
  115. </NameScope.NameScope><NameScope.NameScope>
  116.     <NameScope />
  117. </NameScope.NameScope>   blazorserver-empty   [C#]<NameScope.NameScope>
  118.     <NameScope />
  119. </NameScope.NameScope>  Web/Blazor/Empty
  120. Blazor WebAssembly 应用<NameScope.NameScope>
  121.     <NameScope />
  122. </NameScope.NameScope><NameScope.NameScope>
  123.     <NameScope />
  124. </NameScope.NameScope><NameScope.NameScope>
  125.     <NameScope />
  126. </NameScope.NameScope>blazorwasm<NameScope.NameScope>
  127.     <NameScope />
  128. </NameScope.NameScope>     [C#]<NameScope.NameScope>
  129.     <NameScope />
  130. </NameScope.NameScope>  Web/Blazor/WebAssembly/PWA
  131. Blazor WebAssembly 应用空<NameScope.NameScope>
  132.     <NameScope />
  133. </NameScope.NameScope><NameScope.NameScope>
  134.     <NameScope />
  135. </NameScope.NameScope>    blazorwasm-empty     [C#]<NameScope.NameScope>
  136.     <NameScope />
  137. </NameScope.NameScope>  Web/Blazor/WebAssembly/PWA/Empty
  138. dotnet gitignore 文件<NameScope.NameScope>
  139.     <NameScope />
  140. </NameScope.NameScope><NameScope.NameScope>
  141.     <NameScope />
  142. </NameScope.NameScope><NameScope.NameScope>
  143.     <NameScope />
  144. </NameScope.NameScope>  gitignore<NameScope.NameScope>
  145.     <NameScope />
  146. </NameScope.NameScope><NameScope.NameScope>
  147.     <NameScope />
  148. </NameScope.NameScope><NameScope.NameScope>
  149.     <NameScope />
  150. </NameScope.NameScope><NameScope.NameScope>
  151.     <NameScope />
  152. </NameScope.NameScope>Config
  153. Dotnet 本地工具清单文件<NameScope.NameScope>
  154.     <NameScope />
  155. </NameScope.NameScope><NameScope.NameScope>
  156.     <NameScope />
  157. </NameScope.NameScope><NameScope.NameScope>
  158.     <NameScope />
  159. </NameScope.NameScope>tool-manifest<NameScope.NameScope>
  160.     <NameScope />
  161. </NameScope.NameScope><NameScope.NameScope>
  162.     <NameScope />
  163. </NameScope.NameScope><NameScope.NameScope>
  164.     <NameScope />
  165. </NameScope.NameScope>  Config
  166. EditorConfig 文件<NameScope.NameScope>
  167.     <NameScope />
  168. </NameScope.NameScope><NameScope.NameScope>
  169.     <NameScope />
  170. </NameScope.NameScope><NameScope.NameScope>
  171.     <NameScope />
  172. </NameScope.NameScope><NameScope.NameScope>
  173.     <NameScope />
  174. </NameScope.NameScope>editorconfig<NameScope.NameScope>
  175.     <NameScope />
  176. </NameScope.NameScope><NameScope.NameScope>
  177.     <NameScope />
  178. </NameScope.NameScope><NameScope.NameScope>
  179.     <NameScope />
  180. </NameScope.NameScope>   Config
  181. global.json file<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> globaljson<NameScope.NameScope>
  190.     <NameScope />
  191. </NameScope.NameScope><NameScope.NameScope>
  192.     <NameScope />
  193. </NameScope.NameScope><NameScope.NameScope>
  194.     <NameScope />
  195. </NameScope.NameScope>     Config
  196. MSBuild Directory.Build.props 文件<NameScope.NameScope>
  197.     <NameScope />
  198. </NameScope.NameScope> buildprops<NameScope.NameScope>
  199.     <NameScope />
  200. </NameScope.NameScope><NameScope.NameScope>
  201.     <NameScope />
  202. </NameScope.NameScope><NameScope.NameScope>
  203.     <NameScope />
  204. </NameScope.NameScope>     MSBuild/props
  205. MSBuild Directory.Build.targets 文件     buildtargets<NameScope.NameScope>
  206.     <NameScope />
  207. </NameScope.NameScope><NameScope.NameScope>
  208.     <NameScope />
  209. </NameScope.NameScope><NameScope.NameScope>
  210.     <NameScope />
  211. </NameScope.NameScope>   MSBuild/props
  212. MSTest Test Project<NameScope.NameScope>
  213.     <NameScope />
  214. </NameScope.NameScope><NameScope.NameScope>
  215.     <NameScope />
  216. </NameScope.NameScope><NameScope.NameScope>
  217.     <NameScope />
  218. </NameScope.NameScope>    mstest<NameScope.NameScope>
  219.     <NameScope />
  220. </NameScope.NameScope><NameScope.NameScope>
  221.     <NameScope />
  222. </NameScope.NameScope>   [C#],F#,VB  Test/MSTest
  223. MVC ViewImports<NameScope.NameScope>
  224.     <NameScope />
  225. </NameScope.NameScope><NameScope.NameScope>
  226.     <NameScope />
  227. </NameScope.NameScope><NameScope.NameScope>
  228.     <NameScope />
  229. </NameScope.NameScope><NameScope.NameScope>
  230.     <NameScope />
  231. </NameScope.NameScope>  viewimports<NameScope.NameScope>
  232.     <NameScope />
  233. </NameScope.NameScope>    [C#]<NameScope.NameScope>
  234.     <NameScope />
  235. </NameScope.NameScope>  Web/ASP.NET
  236. MVC ViewStart<NameScope.NameScope>
  237.     <NameScope />
  238. </NameScope.NameScope><NameScope.NameScope>
  239.     <NameScope />
  240. </NameScope.NameScope><NameScope.NameScope>
  241.     <NameScope />
  242. </NameScope.NameScope><NameScope.NameScope>
  243.     <NameScope />
  244. </NameScope.NameScope>    viewstart<NameScope.NameScope>
  245.     <NameScope />
  246. </NameScope.NameScope><NameScope.NameScope>
  247.     <NameScope />
  248. </NameScope.NameScope>[C#]<NameScope.NameScope>
  249.     <NameScope />
  250. </NameScope.NameScope>  Web/ASP.NET
  251. NuGet 配置<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><NameScope.NameScope>
  260.     <NameScope />
  261. </NameScope.NameScope> nugetconfig<NameScope.NameScope>
  262.     <NameScope />
  263. </NameScope.NameScope><NameScope.NameScope>
  264.     <NameScope />
  265. </NameScope.NameScope><NameScope.NameScope>
  266.     <NameScope />
  267. </NameScope.NameScope>    Config
  268. NUnit 3 Test Item<NameScope.NameScope>
  269.     <NameScope />
  270. </NameScope.NameScope><NameScope.NameScope>
  271.     <NameScope />
  272. </NameScope.NameScope><NameScope.NameScope>
  273.     <NameScope />
  274. </NameScope.NameScope><NameScope.NameScope>
  275.     <NameScope />
  276. </NameScope.NameScope>nunit-test<NameScope.NameScope>
  277.     <NameScope />
  278. </NameScope.NameScope>     [C#],F#,VB  Test/NUnit
  279. NUnit 3 Test Project<NameScope.NameScope>
  280.     <NameScope />
  281. </NameScope.NameScope><NameScope.NameScope>
  282.     <NameScope />
  283. </NameScope.NameScope><NameScope.NameScope>
  284.     <NameScope />
  285. </NameScope.NameScope>   nunit<NameScope.NameScope>
  286.     <NameScope />
  287. </NameScope.NameScope><NameScope.NameScope>
  288.     <NameScope />
  289. </NameScope.NameScope>    [C#],F#,VB  Test/NUnit
  290. Razor 类库<NameScope.NameScope>
  291.     <NameScope />
  292. </NameScope.NameScope><NameScope.NameScope>
  293.     <NameScope />
  294. </NameScope.NameScope><NameScope.NameScope>
  295.     <NameScope />
  296. </NameScope.NameScope><NameScope.NameScope>
  297.     <NameScope />
  298. </NameScope.NameScope><NameScope.NameScope>
  299.     <NameScope />
  300. </NameScope.NameScope> razorclasslib<NameScope.NameScope>
  301.     <NameScope />
  302. </NameScope.NameScope>  [C#]<NameScope.NameScope>
  303.     <NameScope />
  304. </NameScope.NameScope>  Web/Razor/Library
  305. Razor 组件<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> razorcomponent<NameScope.NameScope>
  316.     <NameScope />
  317. </NameScope.NameScope> [C#]<NameScope.NameScope>
  318.     <NameScope />
  319. </NameScope.NameScope>  Web/ASP.NET
  320. Razor 页面<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><NameScope.NameScope>
  329.     <NameScope />
  330. </NameScope.NameScope> page<NameScope.NameScope>
  331.     <NameScope />
  332. </NameScope.NameScope><NameScope.NameScope>
  333.     <NameScope />
  334. </NameScope.NameScope>     [C#]<NameScope.NameScope>
  335.     <NameScope />
  336. </NameScope.NameScope>  Web/ASP.NET
  337. Web 配置<NameScope.NameScope>
  338.     <NameScope />
  339. </NameScope.NameScope><NameScope.NameScope>
  340.     <NameScope />
  341. </NameScope.NameScope><NameScope.NameScope>
  342.     <NameScope />
  343. </NameScope.NameScope><NameScope.NameScope>
  344.     <NameScope />
  345. </NameScope.NameScope><NameScope.NameScope>
  346.     <NameScope />
  347. </NameScope.NameScope>   webconfig<NameScope.NameScope>
  348.     <NameScope />
  349. </NameScope.NameScope><NameScope.NameScope>
  350.     <NameScope />
  351. </NameScope.NameScope><NameScope.NameScope>
  352.     <NameScope />
  353. </NameScope.NameScope><NameScope.NameScope>
  354.     <NameScope />
  355. </NameScope.NameScope>Config
  356. Windows 窗体应用<NameScope.NameScope>
  357.     <NameScope />
  358. </NameScope.NameScope><NameScope.NameScope>
  359.     <NameScope />
  360. </NameScope.NameScope><NameScope.NameScope>
  361.     <NameScope />
  362. </NameScope.NameScope><NameScope.NameScope>
  363.     <NameScope />
  364. </NameScope.NameScope> winforms<NameScope.NameScope>
  365.     <NameScope />
  366. </NameScope.NameScope><NameScope.NameScope>
  367.     <NameScope />
  368. </NameScope.NameScope> [C#],VB     Common/WinForms
  369. Windows 窗体控件库<NameScope.NameScope>
  370.     <NameScope />
  371. </NameScope.NameScope><NameScope.NameScope>
  372.     <NameScope />
  373. </NameScope.NameScope><NameScope.NameScope>
  374.     <NameScope />
  375. </NameScope.NameScope>     winformscontrollib   [C#],VB     Common/WinForms
  376. Windows 窗体类库<NameScope.NameScope>
  377.     <NameScope />
  378. </NameScope.NameScope><NameScope.NameScope>
  379.     <NameScope />
  380. </NameScope.NameScope><NameScope.NameScope>
  381.     <NameScope />
  382. </NameScope.NameScope><NameScope.NameScope>
  383.     <NameScope />
  384. </NameScope.NameScope> winformslib<NameScope.NameScope>
  385.     <NameScope />
  386. </NameScope.NameScope>    [C#],VB     Common/WinForms
  387. WPF 应用程序<NameScope.NameScope>
  388.     <NameScope />
  389. </NameScope.NameScope><NameScope.NameScope>
  390.     <NameScope />
  391. </NameScope.NameScope><NameScope.NameScope>
  392.     <NameScope />
  393. </NameScope.NameScope><NameScope.NameScope>
  394.     <NameScope />
  395. </NameScope.NameScope>     wpf<NameScope.NameScope>
  396.     <NameScope />
  397. </NameScope.NameScope><NameScope.NameScope>
  398.     <NameScope />
  399. </NameScope.NameScope><NameScope.NameScope>
  400.     <NameScope />
  401. </NameScope.NameScope>[C#],VB     Common/WPF
  402. WPF 用户控件库<NameScope.NameScope>
  403.     <NameScope />
  404. </NameScope.NameScope><NameScope.NameScope>
  405.     <NameScope />
  406. </NameScope.NameScope><NameScope.NameScope>
  407.     <NameScope />
  408. </NameScope.NameScope><NameScope.NameScope>
  409.     <NameScope />
  410. </NameScope.NameScope>   wpfusercontrollib    [C#],VB     Common/WPF
  411. WPF 类库<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><NameScope.NameScope>
  420.     <NameScope />
  421. </NameScope.NameScope>   wpflib<NameScope.NameScope>
  422.     <NameScope />
  423. </NameScope.NameScope><NameScope.NameScope>
  424.     <NameScope />
  425. </NameScope.NameScope>   [C#],VB     Common/WPF
  426. WPF 自定义控件库<NameScope.NameScope>
  427.     <NameScope />
  428. </NameScope.NameScope><NameScope.NameScope>
  429.     <NameScope />
  430. </NameScope.NameScope><NameScope.NameScope>
  431.     <NameScope />
  432. </NameScope.NameScope><NameScope.NameScope>
  433.     <NameScope />
  434. </NameScope.NameScope> wpfcustomcontrollib  [C#],VB     Common/WPF
  435. xUnit Test Project<NameScope.NameScope>
  436.     <NameScope />
  437. </NameScope.NameScope><NameScope.NameScope>
  438.     <NameScope />
  439. </NameScope.NameScope><NameScope.NameScope>
  440.     <NameScope />
  441. </NameScope.NameScope>     xunit<NameScope.NameScope>
  442.     <NameScope />
  443. </NameScope.NameScope><NameScope.NameScope>
  444.     <NameScope />
  445. </NameScope.NameScope>    [C#],F#,VB  Test/xUnit
  446. 协议缓冲区文件<NameScope.NameScope>
  447.     <NameScope />
  448. </NameScope.NameScope><NameScope.NameScope>
  449.     <NameScope />
  450. </NameScope.NameScope><NameScope.NameScope>
  451.     <NameScope />
  452. </NameScope.NameScope><NameScope.NameScope>
  453.     <NameScope />
  454. </NameScope.NameScope>   proto<NameScope.NameScope>
  455.     <NameScope />
  456. </NameScope.NameScope><NameScope.NameScope>
  457.     <NameScope />
  458. </NameScope.NameScope><NameScope.NameScope>
  459.     <NameScope />
  460. </NameScope.NameScope><NameScope.NameScope>
  461.     <NameScope />
  462. </NameScope.NameScope>    Web/gRPC
  463. 接口<NameScope.NameScope>
  464.     <NameScope />
  465. </NameScope.NameScope><NameScope.NameScope>
  466.     <NameScope />
  467. </NameScope.NameScope><NameScope.NameScope>
  468.     <NameScope />
  469. </NameScope.NameScope><NameScope.NameScope>
  470.     <NameScope />
  471. </NameScope.NameScope><NameScope.NameScope>
  472.     <NameScope />
  473. </NameScope.NameScope><NameScope.NameScope>
  474.     <NameScope />
  475. </NameScope.NameScope> interface<NameScope.NameScope>
  476.     <NameScope />
  477. </NameScope.NameScope><NameScope.NameScope>
  478.     <NameScope />
  479. </NameScope.NameScope>[C#],VB     Common
  480. 控制台应用<NameScope.NameScope>
  481.     <NameScope />
  482. </NameScope.NameScope><NameScope.NameScope>
  483.     <NameScope />
  484. </NameScope.NameScope><NameScope.NameScope>
  485.     <NameScope />
  486. </NameScope.NameScope><NameScope.NameScope>
  487.     <NameScope />
  488. </NameScope.NameScope><NameScope.NameScope>
  489.     <NameScope />
  490. </NameScope.NameScope> console<NameScope.NameScope>
  491.     <NameScope />
  492. </NameScope.NameScope><NameScope.NameScope>
  493.     <NameScope />
  494. </NameScope.NameScope>  [C#],F#,VB  Common/Console
  495. 枚举<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><NameScope.NameScope>
  504.     <NameScope />
  505. </NameScope.NameScope><NameScope.NameScope>
  506.     <NameScope />
  507. </NameScope.NameScope> enum<NameScope.NameScope>
  508.     <NameScope />
  509. </NameScope.NameScope><NameScope.NameScope>
  510.     <NameScope />
  511. </NameScope.NameScope>     [C#],VB     Common
  512. 类<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>   class<NameScope.NameScope>
  525.     <NameScope />
  526. </NameScope.NameScope><NameScope.NameScope>
  527.     <NameScope />
  528. </NameScope.NameScope>    [C#],VB     Common
  529. 类库<NameScope.NameScope>
  530.     <NameScope />
  531. </NameScope.NameScope><NameScope.NameScope>
  532.     <NameScope />
  533. </NameScope.NameScope><NameScope.NameScope>
  534.     <NameScope />
  535. </NameScope.NameScope><NameScope.NameScope>
  536.     <NameScope />
  537. </NameScope.NameScope><NameScope.NameScope>
  538.     <NameScope />
  539. </NameScope.NameScope><NameScope.NameScope>
  540.     <NameScope />
  541. </NameScope.NameScope> classlib<NameScope.NameScope>
  542.     <NameScope />
  543. </NameScope.NameScope><NameScope.NameScope>
  544.     <NameScope />
  545. </NameScope.NameScope> [C#],F#,VB  Common/Library
  546. 结构<NameScope.NameScope>
  547.     <NameScope />
  548. </NameScope.NameScope><NameScope.NameScope>
  549.     <NameScope />
  550. </NameScope.NameScope><NameScope.NameScope>
  551.     <NameScope />
  552. </NameScope.NameScope><NameScope.NameScope>
  553.     <NameScope />
  554. </NameScope.NameScope><NameScope.NameScope>
  555.     <NameScope />
  556. </NameScope.NameScope><NameScope.NameScope>
  557.     <NameScope />
  558. </NameScope.NameScope> struct,structure     [C#],VB     Common
  559. 解决方案文件<NameScope.NameScope>
  560.     <NameScope />
  561. </NameScope.NameScope><NameScope.NameScope>
  562.     <NameScope />
  563. </NameScope.NameScope><NameScope.NameScope>
  564.     <NameScope />
  565. </NameScope.NameScope><NameScope.NameScope>
  566.     <NameScope />
  567. </NameScope.NameScope>     sln,solution<NameScope.NameScope>
  568.     <NameScope />
  569. </NameScope.NameScope><NameScope.NameScope>
  570.     <NameScope />
  571. </NameScope.NameScope><NameScope.NameScope>
  572.     <NameScope />
  573. </NameScope.NameScope>   Solution
  574. 记录<NameScope.NameScope>
  575.     <NameScope />
  576. </NameScope.NameScope><NameScope.NameScope>
  577.     <NameScope />
  578. </NameScope.NameScope><NameScope.NameScope>
  579.     <NameScope />
  580. </NameScope.NameScope><NameScope.NameScope>
  581.     <NameScope />
  582. </NameScope.NameScope><NameScope.NameScope>
  583.     <NameScope />
  584. </NameScope.NameScope><NameScope.NameScope>
  585.     <NameScope />
  586. </NameScope.NameScope> record<NameScope.NameScope>
  587.     <NameScope />
  588. </NameScope.NameScope><NameScope.NameScope>
  589.     <NameScope />
  590. </NameScope.NameScope>   [C#]<NameScope.NameScope>
  591.     <NameScope />
  592. </NameScope.NameScope>  Common
  593. 辅助角色服务<NameScope.NameScope>
  594.     <NameScope />
  595. </NameScope.NameScope><NameScope.NameScope>
  596.     <NameScope />
  597. </NameScope.NameScope><NameScope.NameScope>
  598.     <NameScope />
  599. </NameScope.NameScope><NameScope.NameScope>
  600.     <NameScope />
  601. </NameScope.NameScope>     worker<NameScope.NameScope>
  602.     <NameScope />
  603. </NameScope.NameScope><NameScope.NameScope>
  604.     <NameScope />
  605. </NameScope.NameScope>   [C#],F#     Common/Worker/Web
复制代码
咱们平常用得多的都是前几那几个,比如 mvc、web、wpf、classlib 等。我们在命令中引用的就是项目模板的短名称即可。比如控制台就是 console。
-n 参数指定项目的名称,我这里是“MyApp”,-o 参数指定项目存放目录,“.” 表示当前目录。
接下来要改一下项目文件(*.csproj)。
  1. <Project Sdk="Microsoft.NET.Sdk">
  2.   <PropertyGroup>
  3.     <OutputType>Exe</OutputType>
  4.     <TargetFramework><em><strong>net7.0-windows</strong></em></TargetFramework>
  5.     <ImplicitUsings>enable</ImplicitUsings>
  6.     <Nullable>enable</Nullable>
  7.     <UseWPF><strong><em>true</em></strong></UseWPF>
  8.   </PropertyGroup>
  9. </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.     public MyWindow()
  4.     {
  5. <NameScope.NameScope>
  6.     <NameScope />
  7. </NameScope.NameScope>  InitUI();
  8.     }
  9.     private void InitUI()
  10.     {
  11. <NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope>  // 本窗口的属性
  14. <NameScope.NameScope>
  15.     <NameScope />
  16. </NameScope.NameScope>  this.Title = "鼠爷快乐园";
  17. <NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope>  this.Height = 225;
  20. <NameScope.NameScope>
  21.     <NameScope />
  22. </NameScope.NameScope>  this.Width = 315;
  23. <NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope>  // 启动时窗口在屏幕中央
  26. <NameScope.NameScope>
  27.     <NameScope />
  28. </NameScope.NameScope>  WindowStartupLocation = WindowStartupLocation.CenterScreen;
  29. <NameScope.NameScope>
  30.     <NameScope />
  31. </NameScope.NameScope>  // 整点控件
  32. <NameScope.NameScope>
  33.     <NameScope />
  34. </NameScope.NameScope>  // 两个block
  35. <NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope>  TextBlock tb1 = new()
  38. <NameScope.NameScope>
  39.     <NameScope />
  40. </NameScope.NameScope>  {
  41. <NameScope.NameScope>
  42.     <NameScope />
  43. </NameScope.NameScope><NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope>Text = "每天毙一鼠",
  46. <NameScope.NameScope>
  47.     <NameScope />
  48. </NameScope.NameScope><NameScope.NameScope>
  49.     <NameScope />
  50. </NameScope.NameScope>TextAlignment = TextAlignment.Center,
  51. <NameScope.NameScope>
  52.     <NameScope />
  53. </NameScope.NameScope><NameScope.NameScope>
  54.     <NameScope />
  55. </NameScope.NameScope>// 文本颜色
  56. <NameScope.NameScope>
  57.     <NameScope />
  58. </NameScope.NameScope><NameScope.NameScope>
  59.     <NameScope />
  60. </NameScope.NameScope>Foreground = new SolidColorBrush(Color.FromRgb(12, 50, 208))
  61. <NameScope.NameScope>
  62.     <NameScope />
  63. </NameScope.NameScope>  };
  64. <NameScope.NameScope>
  65.     <NameScope />
  66. </NameScope.NameScope>  TextBlock tb2 = new()
  67. <NameScope.NameScope>
  68.     <NameScope />
  69. </NameScope.NameScope>  {
  70. <NameScope.NameScope>
  71.     <NameScope />
  72. </NameScope.NameScope><NameScope.NameScope>
  73.     <NameScope />
  74. </NameScope.NameScope>Text = "添寿又增福",
  75. <NameScope.NameScope>
  76.     <NameScope />
  77. </NameScope.NameScope><NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope>TextAlignment = TextAlignment.Center
  80. <NameScope.NameScope>
  81.     <NameScope />
  82. </NameScope.NameScope>  };
  83. <NameScope.NameScope>
  84.     <NameScope />
  85. </NameScope.NameScope>  // 再加一个按钮
  86. <NameScope.NameScope>
  87.     <NameScope />
  88. </NameScope.NameScope>  Button btn = new()
  89. <NameScope.NameScope>
  90.     <NameScope />
  91. </NameScope.NameScope>  {
  92. <NameScope.NameScope>
  93.     <NameScope />
  94. </NameScope.NameScope><NameScope.NameScope>
  95.     <NameScope />
  96. </NameScope.NameScope>Content = "行动起来",
  97. <NameScope.NameScope>
  98.     <NameScope />
  99. </NameScope.NameScope><NameScope.NameScope>
  100.     <NameScope />
  101. </NameScope.NameScope>Margin = new Thickness(0d, 15d, 0d, 2d)
  102. <NameScope.NameScope>
  103.     <NameScope />
  104. </NameScope.NameScope>  };
  105. <NameScope.NameScope>
  106.     <NameScope />
  107. </NameScope.NameScope>  // 单击事件
  108. <NameScope.NameScope>
  109.     <NameScope />
  110. </NameScope.NameScope>  btn.Click += OnClick;
  111. <NameScope.NameScope>
  112.     <NameScope />
  113. </NameScope.NameScope>  // 布局控件
  114. <NameScope.NameScope>
  115.     <NameScope />
  116. </NameScope.NameScope>  StackPanel panel = new();
  117. <NameScope.NameScope>
  118.     <NameScope />
  119. </NameScope.NameScope>  // 垂直方向
  120. <NameScope.NameScope>
  121.     <NameScope />
  122. </NameScope.NameScope>  panel.Orientation = Orientation.Vertical;
  123. <NameScope.NameScope>
  124.     <NameScope />
  125. </NameScope.NameScope>  // 添加子元素
  126. <NameScope.NameScope>
  127.     <NameScope />
  128. </NameScope.NameScope>  panel.Children.Add(tb1);
  129. <NameScope.NameScope>
  130.     <NameScope />
  131. </NameScope.NameScope>  panel.Children.Add(tb2);
  132. <NameScope.NameScope>
  133.     <NameScope />
  134. </NameScope.NameScope>  panel.Children.Add(btn);
  135. <NameScope.NameScope>
  136.     <NameScope />
  137. </NameScope.NameScope>  // 作为窗口的内容
  138. <NameScope.NameScope>
  139.     <NameScope />
  140. </NameScope.NameScope>  this.Content = panel;
  141.     }
  142.     private void OnClick(object sender, RoutedEventArgs e)
  143.     {
  144. <NameScope.NameScope>
  145.     <NameScope />
  146. </NameScope.NameScope>  MessageBox.Show("自在其间乐");
  147.     }
  148. }
复制代码
Windows 属于内容控件,公开 Content 属性,用来设置单个对象引用。上述代码先创建两个 TextBlock 实例和一个 Button 实例,然后把它们塞进 StackPanel 中,再把 StackPanel 实例赋值给窗口的 Content 属性。
窗口类写好后,在 Main 方法中,调用 Run 方法时把窗口实例传进去。
  1.     [STAThread]
  2.     static void Main(string[] args)
  3.     {
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>  Application app = new();
  7. <NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>  app.Run(new MyWindow());
  10.     }
复制代码
这个程序已经可以运行了。

 
************************************************************************************************
咱们继续探索。如果要用到数据绑定呢。在 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.     public int ID { get; set; }
  4.     public string? Name { get; set; }
  5.     public int Age { get; set; }
  6. }
复制代码
窗口的结构:内容根为 Grid 对象,它包含三行两列,用来放六个 TextBlock 控件。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>  Grid layout = new();
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>  // 设置为窗口内容
  7. <NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>  this.Content = layout;
  10. <NameScope.NameScope>
  11.     <NameScope />
  12. </NameScope.NameScope>  // 设置边距
  13. <NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope>  layout.Margin = new Thickness(13.5d);
  16. <NameScope.NameScope>
  17.     <NameScope />
  18. </NameScope.NameScope>  // 三行两列
  19. <NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope>  layout.ColumnDefinitions.Add(new ColumnDefinition()
  22. <NameScope.NameScope>
  23.     <NameScope />
  24. </NameScope.NameScope>  {
  25. <NameScope.NameScope>
  26.     <NameScope />
  27. </NameScope.NameScope><NameScope.NameScope>
  28.     <NameScope />
  29. </NameScope.NameScope>Width = GridLength.Auto
  30. <NameScope.NameScope>
  31.     <NameScope />
  32. </NameScope.NameScope>  });
  33. <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>// 星号,即 1*
  41. <NameScope.NameScope>
  42.     <NameScope />
  43. </NameScope.NameScope><NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope>Width = new GridLength(1.0d, GridUnitType.Star)
  46. <NameScope.NameScope>
  47.     <NameScope />
  48. </NameScope.NameScope>  });
  49. <NameScope.NameScope>
  50.     <NameScope />
  51. </NameScope.NameScope>  layout.RowDefinitions.Add(new()
  52. <NameScope.NameScope>
  53.     <NameScope />
  54. </NameScope.NameScope>  {
  55. <NameScope.NameScope>
  56.     <NameScope />
  57. </NameScope.NameScope><NameScope.NameScope>
  58.     <NameScope />
  59. </NameScope.NameScope>Height = GridLength.Auto
  60. <NameScope.NameScope>
  61.     <NameScope />
  62. </NameScope.NameScope>  });
  63. <NameScope.NameScope>
  64.     <NameScope />
  65. </NameScope.NameScope>  layout.RowDefinitions.Add(new()
  66. <NameScope.NameScope>
  67.     <NameScope />
  68. </NameScope.NameScope>  {
  69. <NameScope.NameScope>
  70.     <NameScope />
  71. </NameScope.NameScope><NameScope.NameScope>
  72.     <NameScope />
  73. </NameScope.NameScope>Height = GridLength.Auto
  74. <NameScope.NameScope>
  75.     <NameScope />
  76. </NameScope.NameScope>  });
  77. <NameScope.NameScope>
  78.     <NameScope />
  79. </NameScope.NameScope>  layout.RowDefinitions.Add(new()
  80. <NameScope.NameScope>
  81.     <NameScope />
  82. </NameScope.NameScope>  {
  83. <NameScope.NameScope>
  84.     <NameScope />
  85. </NameScope.NameScope><NameScope.NameScope>
  86.     <NameScope />
  87. </NameScope.NameScope>Height = GridLength.Auto
  88. <NameScope.NameScope>
  89.     <NameScope />
  90. </NameScope.NameScope>  });
复制代码
ColumnDefinitions 用来定义列。上述代码中,第一列的宽度为 Auto,第二列的宽度为 *。
RowDefinitions 集合用来定义行。上述代码中,三行的高度都是 Auto。
然后 new 出六个 TextBlock,三个用来显示字段标签,三个用于数据绑定,显示属性值。
  1. <NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>  // 六个block
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>  var tbIDf = new TextBlock(){
  7. <NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope><NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope>Text = "学号:"
  12. <NameScope.NameScope>
  13.     <NameScope />
  14. </NameScope.NameScope>  };
  15. <NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>  var tbNamef = new TextBlock()
  18. <NameScope.NameScope>
  19.     <NameScope />
  20. </NameScope.NameScope>  {
  21. <NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope><NameScope.NameScope>
  24.     <NameScope />
  25. </NameScope.NameScope>Text = "姓名:"
  26. <NameScope.NameScope>
  27.     <NameScope />
  28. </NameScope.NameScope>  };
  29. <NameScope.NameScope>
  30.     <NameScope />
  31. </NameScope.NameScope>  var tbAgef = new TextBlock()
  32. <NameScope.NameScope>
  33.     <NameScope />
  34. </NameScope.NameScope>  {
  35. <NameScope.NameScope>
  36.     <NameScope />
  37. </NameScope.NameScope><NameScope.NameScope>
  38.     <NameScope />
  39. </NameScope.NameScope>Text = "年龄:"
  40. <NameScope.NameScope>
  41.     <NameScope />
  42. </NameScope.NameScope>  };
  43. <NameScope.NameScope>
  44.     <NameScope />
  45. </NameScope.NameScope>  TextBlock tbIDv = new();
  46. <NameScope.NameScope>
  47.     <NameScope />
  48. </NameScope.NameScope>  var tbNamev = new TextBlock();
  49. <NameScope.NameScope>
  50.     <NameScope />
  51. </NameScope.NameScope>  var tbAgev = new TextBlock();
复制代码
把六个 TextBlock 控件添加到 Grid 的子级中。
  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);
复制代码
设置子元素所在行、列的 Grid.Row 和 Grid.Column 是附加属性,以 Grid.SetXXX 方法调用。
  1.     layout.Children.Add(tbIDf);
  2.     layout.Children.Add(tbIDv);
  3.     layout.Children.Add(tbNamef);
  4.     layout.Children.Add(tbNamev);
  5.     layout.Children.Add(tbAgef);
  6.     layout.Children.Add(tbAgev);
  7.     // Row、Column 附加属性
  8.     // 第一行第一列
  9.     Grid.SetRow(tbIDf, 0);
  10.     Grid.SetColumn(tbIDf, 0);
  11.     // 第一行第二列
  12.     Grid.SetRow(tbIDv, 0);
  13.     Grid.SetColumn(tbIDv, 1);
  14.     // 第二行第一列
  15.     Grid.SetRow(tbNamef, 1);
  16.     Grid.SetColumn(tbNamef, 0);
  17.     // 第二行第二列
  18.     Grid.SetRow(tbNamev, 1);
  19.     Grid.SetColumn(tbNamev, 1);
  20.     // 第三行第一列
  21.     Grid.SetRow(tbAgef, 2);
  22.     Grid.SetColumn(tbAgef,0);
  23.     // 第三行第二列
  24.     Grid.SetRow(tbAgev, 2);
  25.     Grid.SetColumn(tbAgev, 1);
复制代码
最后是创建三个 Binding ,为 Student 类的三个属性做绑定。
  1.     /* ID */
  2.     Binding bindID = new(nameof(Student.ID))
  3.     {
  4. <NameScope.NameScope>
  5.     <NameScope />
  6. </NameScope.NameScope>  Source = this.stu
  7.     };
  8.     BindingOperations.SetBinding(tbIDv, TextBlock.TextProperty, bindID);
  9.     /* Name */
  10.     Binding bindName = new(nameof(Student.Name))
  11.     {
  12. <NameScope.NameScope>
  13.     <NameScope />
  14. </NameScope.NameScope>  Source = stu
  15.     };
  16.     BindingOperations.SetBinding(tbNamev, TextBlock.TextProperty,
  17. bindName);
  18.     /* Age */
  19.     Binding bindAge = new(nameof(Student.Age))
  20.     {
  21. <NameScope.NameScope>
  22.     <NameScope />
  23. </NameScope.NameScope>  Source = stu
  24.     };
  25.     BindingOperations.SetBinding(tbAgev, TextBlock.TextProperty, bindAge);
复制代码
完整的初始化方法代码如下:
  1. private void InitUI() {     Title = "数据绑定";     Width = 240;     Height = 185;     // 创建Grid     Grid layout = new();     // 设置为窗口内容     this.Content = layout;     // 设置边距     layout.Margin = new Thickness(13.5d);     // 三行两列     layout.ColumnDefinitions.Add(new ColumnDefinition()     {<NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>   Width = GridLength.Auto     });     layout.ColumnDefinitions.Add(new ColumnDefinition(){<NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>   // 星号,即 1*<NameScope.NameScope>
  6.     <NameScope />
  7. </NameScope.NameScope>   Width = new GridLength(1.0d, GridUnitType.Star)     });     layout.RowDefinitions.Add(new()     {<NameScope.NameScope>
  8.     <NameScope />
  9. </NameScope.NameScope>   Height = GridLength.Auto     });     layout.RowDefinitions.Add(new()     {<NameScope.NameScope>
  10.     <NameScope />
  11. </NameScope.NameScope>   Height = GridLength.Auto     });     layout.RowDefinitions.Add(new()     {<NameScope.NameScope>
  12.     <NameScope />
  13. </NameScope.NameScope>   Height = GridLength.Auto     });<NameScope.NameScope>
  14.     <NameScope />
  15. </NameScope.NameScope>    // 六个block     var tbIDf = new TextBlock(){<NameScope.NameScope>
  16.     <NameScope />
  17. </NameScope.NameScope>   Text = "学号:"     };     var tbNamef = new TextBlock()     {<NameScope.NameScope>
  18.     <NameScope />
  19. </NameScope.NameScope>   Text = "姓名:"     };     var tbAgef = new TextBlock()     {<NameScope.NameScope>
  20.     <NameScope />
  21. </NameScope.NameScope>   Text = "年龄:"     };     TextBlock tbIDv = new();     var tbNamev = new TextBlock();     var tbAgev = new TextBlock();     // 把六个block放到 grid 上     layout.Children.Add(tbIDf);
  22.     layout.Children.Add(tbIDv);
  23.     layout.Children.Add(tbNamef);
  24.     layout.Children.Add(tbNamev);
  25.     layout.Children.Add(tbAgef);
  26.     layout.Children.Add(tbAgev);
  27.     // Row、Column 附加属性
  28.     // 第一行第一列
  29.     Grid.SetRow(tbIDf, 0);
  30.     Grid.SetColumn(tbIDf, 0);
  31.     // 第一行第二列
  32.     Grid.SetRow(tbIDv, 0);
  33.     Grid.SetColumn(tbIDv, 1);
  34.     // 第二行第一列
  35.     Grid.SetRow(tbNamef, 1);
  36.     Grid.SetColumn(tbNamef, 0);
  37.     // 第二行第二列
  38.     Grid.SetRow(tbNamev, 1);
  39.     Grid.SetColumn(tbNamev, 1);
  40.     // 第三行第一列
  41.     Grid.SetRow(tbAgef, 2);
  42.     Grid.SetColumn(tbAgef,0);
  43.     // 第三行第二列
  44.     Grid.SetRow(tbAgev, 2);
  45.     Grid.SetColumn(tbAgev, 1);     // 数据绑定     /* ID */
  46.     Binding bindID = new(nameof(Student.ID))
  47.     {
  48. <NameScope.NameScope>
  49.     <NameScope />
  50. </NameScope.NameScope>  Source = this.stu
  51.     };
  52.     BindingOperations.SetBinding(tbIDv, TextBlock.TextProperty, bindID);
  53.     /* Name */
  54.     Binding bindName = new(nameof(Student.Name))
  55.     {
  56. <NameScope.NameScope>
  57.     <NameScope />
  58. </NameScope.NameScope>  Source = stu
  59.     };
  60.     BindingOperations.SetBinding(tbNamev, TextBlock.TextProperty,
  61. bindName);
  62.     /* Age */
  63.     Binding bindAge = new(nameof(Student.Age))
  64.     {
  65. <NameScope.NameScope>
  66.     <NameScope />
  67. </NameScope.NameScope>  Source = stu
  68.     };
  69.     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() {     Title = "元素之间绑定";     // 根据内容自动调整窗口大小     SizeToContent = SizeToContent.WidthAndHeight;     StackPanel panel = new(){<NameScope.NameScope>
  2.     <NameScope />
  3. </NameScope.NameScope>   Orientation = Orientation.Vertical,<NameScope.NameScope>
  4.     <NameScope />
  5. </NameScope.NameScope>   Margin = new Thickness(15.0d)     };     this.Content = panel;   // 布局     // 文本输入控件     TextBox txt = new TextBox();     txt.Margin = new Thickness(3.0d, 5.0d, 3.0d, 8.5d);     // 给它分配一个名字,绑定时用到     NameScope myScope = new();     NameScope.SetNameScope(this, myScope);     myScope.RegisterName("txtInput", txt);     // 文本块     TextBlock tb = new TextBlock();     tb.Margin = new Thickness(5.0d, 0d, 5.0d, 0d);     // 绑定     Binding bind = new();     bind.ElementName = "txtInput";     bind.Path = new PropertyPath(TextBox.TextProperty);     bind.Mode = BindingMode.OneWay;     // 在文本框更改时更新数据     bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;     BindingOperations.SetBinding(tb, TextBlock.TextProperty, bind);     // 添加到布局     panel.Children.Add(txt);     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 什么的,咱们下次再探讨。
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

祗疼妳一个

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表