.NET 6为WinForms带来了哪些颠覆性的新功能?

2026-04-13 18:1214阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计510个文字,预计阅读时间需要3分钟。

.NET 6为WinForms带来了哪些颠覆性的新功能?

首先简化了Program文件,引入了全局命名空间,但顶级语句因Main函数的特性[STAThread]未引用namespace。代码如下:

csharpnamespace WinFormsDemo{ internal static class Program { /// /// The main entry point for the application. /// }}

  首先简化了Program文件,引入了全局命名空间,但顶级语句由于Main函数的特性[STAThread]没有引用进来。

namespace WinFormsDemo
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new frmMain());
}
}
}

ApplicationConfiguration.Initialize,其实是进行了一个封装,代码如下:

using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace WinFormsDemo
{
/// <summary>
/// Bootstrap the application configuration.
/// </summary>
[CompilerGenerated]
internal static partial class ApplicationConfiguration
{
/// <summary>
/// Bootstrap the application as follows:
/// <code>
/// Application.EnableVisualStyles();
/// Application.SetCompatibleTextRenderingDefault(false);
/// Application.SetHighDpiMode(HighDpiMode.SystemAware);
/// </code>
/// </summary>
public static void Initialize()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
}
}
}

再就是引入了全局字体设置,可以在Main引入,也可以在项目文件中配置:

[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.SetDefaultFont(new Font("汉仪篆书繁", 12));
Application.Run(new frmMain());
}

或(但项目文件中配置发现不如代码中引入,有点变形,这里还需要完善)

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationDefaultFont>汉仪篆书繁, 12pt</ApplicationDefaultFont>
</PropertyGroup>
</Project>

效果如下:

再有就是更好的支持高DPI,还有一些新的PAI和修改过的API,具体参见:docs.microsoft.com/zh-cn/dotnet/desktop/winforms/whats-new/net60?view=netdesktop-6.0

.NET 6为WinForms带来了哪些颠覆性的新功能?

  想要更快更方便的了解相关知识,可以关注微信公众号



本文共计510个文字,预计阅读时间需要3分钟。

.NET 6为WinForms带来了哪些颠覆性的新功能?

首先简化了Program文件,引入了全局命名空间,但顶级语句因Main函数的特性[STAThread]未引用namespace。代码如下:

csharpnamespace WinFormsDemo{ internal static class Program { /// /// The main entry point for the application. /// }}

  首先简化了Program文件,引入了全局命名空间,但顶级语句由于Main函数的特性[STAThread]没有引用进来。

namespace WinFormsDemo
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new frmMain());
}
}
}

ApplicationConfiguration.Initialize,其实是进行了一个封装,代码如下:

using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace WinFormsDemo
{
/// <summary>
/// Bootstrap the application configuration.
/// </summary>
[CompilerGenerated]
internal static partial class ApplicationConfiguration
{
/// <summary>
/// Bootstrap the application as follows:
/// <code>
/// Application.EnableVisualStyles();
/// Application.SetCompatibleTextRenderingDefault(false);
/// Application.SetHighDpiMode(HighDpiMode.SystemAware);
/// </code>
/// </summary>
public static void Initialize()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
}
}
}

再就是引入了全局字体设置,可以在Main引入,也可以在项目文件中配置:

[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.SetDefaultFont(new Font("汉仪篆书繁", 12));
Application.Run(new frmMain());
}

或(但项目文件中配置发现不如代码中引入,有点变形,这里还需要完善)

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationDefaultFont>汉仪篆书繁, 12pt</ApplicationDefaultFont>
</PropertyGroup>
</Project>

效果如下:

再有就是更好的支持高DPI,还有一些新的PAI和修改过的API,具体参见:docs.microsoft.com/zh-cn/dotnet/desktop/winforms/whats-new/net60?view=netdesktop-6.0

.NET 6为WinForms带来了哪些颠覆性的新功能?

  想要更快更方便的了解相关知识,可以关注微信公众号