What’s new for .NET 6 (Windows Forms .NET)
Updated templates for C#
工程模板适配了C#10的global using, file-scoped namespaces, nullable reference types。
默认没有启用top-level statements。因为典型的Winforms应用需要[STAThread]
属性。
New application bootstrap
ApplicationConfiguration.Initialize
变为编译时动态生成:
public static void Initialize()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
}
旧的自生发方式不被Windows Forms Visual Designer所尊重。
Project-level application settings
下列设置变为在工程中进行:
- ApplicationVisualStyles(true)
- ApplicationUseCompatibleTextRendering (false)
- ApplicationHighDpiMode(SystemAware)
- ApplicationDefaultFont(Segoe UI, 9pt)
工程文件示例:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationVisualStyles>true</ApplicationVisualStyles>
<ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
<ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
<ApplicationDefaultFont>Microsoft Sans Serif, 8.25pt</ApplicationDefaultFont>
</PropertyGroup>
</Project>
Change the default font
.NET Framework用的是Microsoft Sans Serif, 8.25pt
。
Visual Studio designer improvements
Designer能反映真实所用的字体了。
More runtime designers
.NET Framework中的需要Designer类型被添加到了.NET 6。
High DPI improvements for PerMonitorV2
可以通过PerMonitorV2支持高DPI。
New APIs
列举了新的API
New Visual Basic APIs
……
Updated APIs
更新了的API
Improved accessibility
……
What’s new for .NET 5 (Windows Forms .NET)
Enhanced features
……
New controls
- System.Windows.Forms.TaskDialog
- Microsoft.Web.WebView2.WinForms.WebView2
Enhanced controls
……
Desktop Guide (Windows Forms .NET)
Winforms有两个实现
- 新的开源的https://github.com/dotnet/winforms
- 老的基于.NET Framework4的,依然在VS2022中支持
本文介绍的是新款的Winforms
Introduction
……
Why migrate from .NET Framework
……
Build rich, interactive user interfaces
一个form是一个可视化界面,可以展示信息给用户。 建构应用的过程是超form上添加控件。 而控件是一个明确的的UI元素,用于展示或者接受数据。
用户输入会产生一个事件,应用通过相应这些事件与用户交互。
可以通过UserControl自定义控件。
一些控件比如FlowLayoutPanel、TableLayoutPanel和SplitContainer可以帮你创建高级的布局。
通过System.Drawing命名控件中的标类可以帮助绘制自定义UI元素所需的线段、圆圈以及其他形状。
Create forms and controls
……
Display and manipulate data
DataGridView有助于显示网格数据。
BindingSource组件可以方便创建到数据源的连接。BindingNavigator可以在BindingSource之上方便浏览数据。
VS的Data Source窗口可以方便用于创建具有数据绑定的的控件。
Application Settings可以让应用方便保持配置到XML文件。
Deploy apps to client computers
ClickOnce可以用于部署应用。
Tutorial: Create a Windows Forms app with .NET
撸了一下,果然很方便。
How to migrate a Windows Forms desktop app to .NET 5
Winforms依然是Windows专有。从.NET Framework迁移到.NET 5总体上需要一个新的Visual Studio工程文件。 .NET SDK的工程文件一般更小。
Try the upgrade assistant
.NET Upgrade Assistant 是一个命令行工具,用于将.NET Framework应用升级到.NET 5。一般升级后,还需要额外的手动配置才能工作。参考Upgrade a WPF App to .NET 5 with the .NET Upgrade Assistant。
Prerequisites
……
Consider
使用.NET Portability Analyzer判断工程是否可以迁移到.NET 5。
有一些老的用编口只有.NET Framework中有,可以使用Windows Compatibility Pack来弥补。
Back up your projects
……
NuGet packages
……
Project file
基于.NET SDK样式的工程文件内容:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType> <!-- Library需要这一项 -->
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
假设工程是MatcingGames,那么要添加下面内容到PropertyGroup:
<RootNamespace>MatchingGame</RootNamespace>
<AssemblyName>MatchingGame</AssemblyName>
将旧工程中的含<ProjectReference>
的<ItemGroup>
部分拷贝过来。
Resources and settings
.NET 5工程默认会包含所有文件,需要显式排除文件,这一点跟.NET Framework工程相反。
Windows Forms工程会引用以下文件:
- Properties\Settings.settings
- Properties\Resources.resx
- Properties\app.manifest
对于*.resx
和*.settings
,它们的旧工程文件设置需要拷贝到当前工程中,并且将<Compile Include="value">
中的Include改成Update。
总之变成这样:
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<Compile Update="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
</ItemGroup>
Visual Basic
……
Edit App.config
如果工程中有App.config,移除其中的<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
。也就是说.NET 5中此文件不用来配置.NET的运行时信息。
Add the compatibility package
添加Microsoft.Windows.Compatibility,是为了解决以下可能出现的问题:
The type or namespace <some name> could not be found
The name <some name> does not exist in the current context
其他
(完)