.NET 7 的新功能 (Windows Forms .NET)
本文說明 .NET 7 中的一些新的 Windows Forms 功能和增強功能。
從 .NET Framework 移轉至 .NET 7 時,您應該注意一些重大變更。 如需詳細資訊,請參閱 Windows Forms 的重大變更。
高 DPI 改善
已改善的高 PerMonitorV2 DPI 轉譯:
正確調整巢狀控制項。 例如,位於面板上的按鈕,放在索引標籤頁面上。
針對 Form.MaximumSize 執行
ApplicationHighDpiMode
設定PerMonitorV2
為 的應用程式,根據目前的監視器 DPI 設定調整和 Form.MinimumSize 屬性。在 .NET 7 中,此功能預設為停用,您必須加入宣告以接收這項變更。 從 .NET 8 開始,此功能預設為啟用,您必須退出宣告此功能以還原為先前的行為。
若要啟用功能,請在
configProperties
runtimeconfig.json 中 設定設定:{ "runtimeOptions": { "tfm": "net7.0", "frameworks": [ ... ], "configProperties": { "System.Windows.Forms.ScaleTopLevelFormMinMaxSizeForDpi": true, } } }
協助工具改善和修正
此版本會進一步改善協助工具,包括但不限於下列專案:
已解決螢幕助讀程式中觀察到的許多公告相關問題,以確保控制項的相關資訊正確無誤。 例如, ListView 現在會在群組展開或折迭時正確宣告。
現在,更多控制項提供消費者介面自動化支援:
已修正在輔助工具下執行 Windows Forms 應用程式的相關記憶體流失,例如朗讀程式。
輔助工具現在準確地繪製焦點指標,並報告巢狀表單和複合控制項的某些元素的正確周框,例如 DataGridView 、 ListView 和 TabControl 。
自動化 UI ExpandCollapse 控制項模式 已在 、 TreeView 和 PropertyGrid 控制項中 ListView 正確實作,而且只會針對可展開的專案啟用。
控制項中的各種色彩對比比例修正。
高對比主題和 ToolStripButton 的可見度改善 ToolStripTextBox 。
資料系結改善 (預覽)
雖然 Windows Forms 已經有強大的系結引擎,但引進了更現代化的資料系結形式,類似于 WPF 所提供的資料系結。
新的資料系結功能可讓您完全採用 MVVM 模式,以及從 Windows Forms 中的 ViewModels 使用物件關聯式對應器比之前更容易。 這反過來又可讓您減少程式碼後置檔案中的程式碼,並開啟新的測試可能性。 更重要的是,它可讓 Windows Forms 與其他 .NET GUI 架構之間的程式碼共用,例如 WPF、UWP/WinUI 和 .NET MAUI。 為了厘清常見問題,Windows Forms 中沒有任何引進 XAML 的計畫。
這些新的資料系結功能目前為 .NET 7 的預覽版,而且在 .NET 8 中將會有更多關於這項功能的工作。
若要啟用新的系結,請將 設定新增 EnablePreviewFeatures
至專案檔。 C# 和 Visual Basic 都支援此功能。
<Project Sdk="Microsoft.NET.Sdk">
<!-- other settings -->
<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
</Project>
下列程式碼片段示範新增至 Windows Forms 中各種類別的新屬性、事件和方法。 雖然下列程式碼範例位於 C# 中,但它也適用于 Visual Basic。
public class Control {
[BindableAttribute(true)]
public virtual object DataContext { get; set; }
[BrowsableAttribute(true)]
public event EventHandler DataContextChanged;
protected virtual void OnDataContextChanged(EventArgs e);
protected virtual void OnParentDataContextChanged(EventArgs e);
}
[RequiresPreviewFeaturesAttribute]
public abstract class BindableComponent : Component, IBindableComponent, IComponent, IDisposable {
protected BindableComponent();
public BindingContext? BindingContext { get; set; }
public ControlBindingsCollection DataBindings { get; }
public event EventHandler BindingContextChanged;
protected virtual void OnBindingContextChanged(EventArgs e);
}
public abstract class ButtonBase : Control {
[BindableAttribute(true)]
[RequiresPreviewFeaturesAttribute]
public ICommand? Command { get; set; }
[BindableAttribute(true)]
public object? CommandParameter { [RequiresPreviewFeaturesAttribute] get; [RequiresPreviewFeaturesAttribute] set; }
[RequiresPreviewFeaturesAttribute]
public event EventHandler? CommandCanExecuteChanged;
[RequiresPreviewFeaturesAttribute]
public event EventHandler? CommandChanged;
[RequiresPreviewFeaturesAttribute]
public event EventHandler? CommandParameterChanged;
[RequiresPreviewFeaturesAttribute]
protected virtual void OnCommandCanExecuteChanged(EventArgs e);
[RequiresPreviewFeaturesAttribute]
protected virtual void OnCommandChanged(EventArgs e);
[RequiresPreviewFeaturesAttribute]
protected virtual void OnCommandParameterChanged(EventArgs e);
[RequiresPreviewFeaturesAttribute]
protected virtual void OnRequestCommandExecute(EventArgs e);
}
public abstract class ToolStripItem : BindableComponent, IComponent, IDisposable, IDropTarget {
[BindableAttribute(true)]
[RequiresPreviewFeaturesAttribute]
public ICommand Command { get; set; }
[BindableAttribute(true)]
public object CommandParameter { [RequiresPreviewFeaturesAttribute] get; [RequiresPreviewFeaturesAttribute] set; }
[RequiresPreviewFeaturesAttribute]
public event EventHandler CommandCanExecuteChanged;
[RequiresPreviewFeaturesAttribute]
public event EventHandler CommandChanged;
[RequiresPreviewFeaturesAttribute]
public event EventHandler CommandParameterChanged;
[RequiresPreviewFeaturesAttribute]
protected virtual void OnCommandCanExecuteChanged(EventArgs e);
[RequiresPreviewFeaturesAttribute]
protected virtual void OnCommandChanged(EventArgs e);
[RequiresPreviewFeaturesAttribute]
protected virtual void OnCommandParameterChanged(EventArgs e);
[RequiresPreviewFeaturesAttribute]
protected virtual void OnRequestCommandExecute(EventArgs e);
}
其他改進項目
以下是一些其他值得注意的變更:
- 拖放處理符合 Windows 拖放功能,具有更豐富的顯示效果,例如圖示和文字標籤。
- 資料夾和檔案對話方塊允許更多選項:
- 新增至最近
- 檢查寫入存取權
- 展開模式
- 確定需要互動
- 選取唯讀
- 顯示隱藏的檔案
- 顯示釘選的位置
- 顯示預覽
- ErrorProvider 現在有 屬性 HasErrors 。
- Windows 11 已修正表單的貼齊版面配置。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應