2020.03.17 追記
Visual Studio 2019 已經更新到16.5.0,已經支持設計模式下拖動控件~~
正文
今天下班回家的路上,在群里發現群友說Visual Studio 2019更新了,貌似支持了WinForm
的designer
文件。回家之后,打開心愛的小本本,將Visual Studio 2019完之后,去巨硬的官方看了一下更新公告。
發現並沒有列出此項作為更新項。又失望的以為群友在晃點我。然后,建立了一個測試工程。發現沙雕群友竟然沒有晃點我~ 讓我們來看看這次更新帶來的意外之喜吧~
建立項目
-
打開VS2019,點擊創建新項目,選擇模板的時候。選擇
Windows Forms App(.NET Core)
。如下圖所示。 -
下一步,填寫項目名稱,選擇項目路徑。
-
下一步,我們的測試項目就創建完成了~
-
點開
Project
文件,查看項目架構~ -
再點看Form1看一下~
看到熟悉的文件了~~
添加控件
我嘗試了一下,發現現在還是無法使用設計模式預覽Designer文件。當然也有可能是我還沒有仔細研究過~不過這也無傷大雅,我們來手動添加控件測試一下。
**注意:Visual Studio Enterprise 2019 Preview ** 16.5.0 Preview 4.0
開始就支持設計模式。附上微軟官方開發人員的Blog地址:一個美女的Blog~
-
添加一個Text控件,整個Designer文件代碼如下。
using System.Windows.Forms; namespace dotnetCoreFromTest { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region this.Controls /// <summary> /// textBox1 /// </summary> private TextBox textBox1; #endregion #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new TextBox(); this.textBox1.Location = new System.Drawing.Point(10, 10); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 21); this.textBox1.TabIndex = 0; this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); this.Text = "Form1"; this.Controls.Add(this.textBox1); } #endregion } }
當然,可能有一部分基礎不是很好的朋友對手擼控件很愁得慌~ 這也完全沒關系。告訴大家一個取巧的辦法。大家可以建立一個.NET Framework的WinForm項目。然后通過設計模式拖一個你想要的控件,設置好屬性后,去Designer文件中將該控件復制到.NET Core 3.1 你相應的Designer文件下就可以啦~
運行測試
都已經弄好了,還等什么呢~ F5運行起來吧~ 效果圖如下。
后記
莫名的興奮。其實我也不知道為啥興奮。因為目前dotnet core
的WinForm
還是沒有辦法投入到生產環境,自身的BUG、第三方控件庫的適配等等問題。但是問題並不能阻擋我們繼續追求更快更省更優的開發之路。希望能在Visual Studio 2019
正式版本16.5.0
上能夠使用完整的WinForm
的功能吧~ 求求了~~