.NET 5重大變更之WPF和Windows Forms

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":".NET 5重大變更系列的最後一個主題是WPF和Windows Forms。這些桌面技術在.NET Core 3.0之前是不可用的,因爲.NET Core的早期版本主要專注於基於Web的應用程序(ASP.NET Core)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2002年.NET首次發佈時,"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Windows_Forms","title":null,"type":null},"content":[{"type":"text","text":"Windows Forms"}]},{"type":"text","text":"(非正式地被稱爲WinForms)是最早的應用程序框架之一。(其他框架包括WebForms、Console和Windows Services。)WinForms深受Visual Basic的影響,和VB一樣,它對原生Windows控件進行了一層薄薄的包裝。默認情況下,這提供了非常好的性能,但定製能力相當有限。一般來說,開發人員應該允許操作系統處理樣式等方面的東西。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"WinForms的第一個主要變化是在2005年發佈的.NET 2.0中出現的,引入了微軟認爲低效的各種用戶控件的替代品,包括基本的數據網格。從那時起,這項技術就被認爲是“完整的”,唯一的變化是偶爾更新一下,以便支持更高的DPI。即使是長期存在的bug通常也會被忽略,這讓使用WinForms進行新項目開發的開發人員感到非常惱火。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Windows_Presentation_Foundation","title":null,"type":null},"content":[{"type":"text","text":"Windows Presentation Foundation"}]},{"type":"text","text":" (WPF)是微軟首次嘗試創建的一個完全可定製的GUI框架。雖然只比WinForms晚發佈了四年,但它還是花了很長一段時間才被普遍接受,因爲它的設計模式和概念更爲複雜,性能也可能是一個問題。雖然有些技術(如虛擬化)可以使WPF在大型數據集上比WinForms快得多,但需要更深入的知識才能正確使用它們。此外,很多開發人員認爲定製樣式是必需的,因爲默認樣式很“醜”。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"WPF有兩個影響深遠的關鍵“特性”,一個是使用XAML進行UI佈局,另一個是使用MVVM模式進行數據綁定。這些也被引入到其他項目中,比如Silverlight和Universal Windows Platform。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"像WinForms一樣,WPF多年來也一直被認爲是“完整的”。它甚至不支持"},{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2009\/12\/XAML-2009-Future\/","title":null,"type":null},"content":[{"type":"text","text":"XAML-2009"}]},{"type":"text","text":",即使是在2012年"},{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2012\/02\/Xaml-VBA-Specs\/?itm_source=presentations_about_XAML-2009&itm_medium=link&itm_campaign=XAML-2009","title":null,"type":null},"content":[{"type":"text","text":"XAML規範"}]},{"type":"text","text":"發佈之後。作爲迴應,"},{"type":"link","attrs":{"href":"http:\/\/avaloniaui.net\/","title":null,"type":null},"content":[{"type":"text","text":"Avalonia"}]},{"type":"text","text":"等項目試圖改進WPF的缺陷,比如允許將事件直接綁定到模型或視圖模型。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"新的SDK"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在.NET Core 3.x中,WPF和WinForms需要一個叫作Microsoft.NET.Sdk.WindowsDesktop的自定義SDK。在.NET 5中,使用的是Microsoft.NET.Sdk,就像其他.NET 5項目一樣。但現在的目標框架是net5.0-windows,而不是net5.0。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"隱藏控制檯輸出"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"絕大多數WPF和WinForms應用程序不顯示控制檯窗口,這是默認的選項。如果你需要一個地方來顯示額外的診斷信息,可以將OutputType設置爲Exe而不是WinExe。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在.NET 5中,除非你將DisableWinExeOutputInference設置爲true,否則OutputType設置將被忽略。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"錯誤處理的改進"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"錯誤處理的改進也可能是個重大變更。在.NET Core 3的Windows Forms中,很多驗證代碼根本不存在。這意味着如果你傳入一個無效的參數,它可能會拋出一個NullReferenceException或者表現出一種不確定的行爲。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在.NET 5中,它會拋出相應的ArgumentException、ArgumentNullException或ArgumentOutOfRangeException。如果是屬性爲空,而不是參數爲空,那麼將拋出InvalidOperationException。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有關如何正確使用異常,請參見"},{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/articles\/Exceptions-API-Design\/","title":null,"type":null},"content":[{"type":"text","text":".NET中的異常設計"}]},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"移除狀態欄控件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在上面討論WinForms的歷史,是爲了解釋狀態欄控件都發生了什麼。在.NET Framework 2中被替換的一個控件是不起眼的"},{"type":"link","attrs":{"href":"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.windows.forms.statusbar?view=netcore-3.1","title":null,"type":null},"content":[{"type":"text","text":"StatusBar"}]},{"type":"text","text":"。在.NET 2中是"},{"type":"link","attrs":{"href":"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.windows.forms.statusstrip?view=net-5.0","title":null,"type":null},"content":[{"type":"text","text":"StatusStrip"}]},{"type":"text","text":",它們的作用是一樣的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"微軟沒有解釋爲什麼要移除它,所以對於一個大多數人都不知道的控制,其維護成本太高了。StatusBar在設計器工具箱中已經很久不可見了,在.NET 5中已經被移除了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本系列其他文章"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/12\/net-5-breaking-changes","title":null,"type":null},"content":[{"type":"text","text":".NET 5基類庫的重大變更"}]},{"type":"text","text":";"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/12\/net-5-breaking-changes-2","title":null,"type":null},"content":[{"type":"text","text":".NET 5重大變化:歷史性的技術"}]},{"type":"text","text":";"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/12\/net-5-breaking-changes-aspnet","title":null,"type":null},"content":[{"type":"text","text":".NET 5 ASP.NET Core重大變更"}]},{"type":"text","text":"。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文鏈接"},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/12\/net-5-breaking-changes-windows\/","title":null,"type":null},"content":[{"type":"text","text":".NET 5 Breaking Changes for WPF\/Windows Forms"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章