共用方式為


.NET 7 的新增功能 (Windows Forms .NET)

本文說明 .NET 7 中一些新的 Windows Forms 功能和增強功能。

從 .NET Framework 移轉至 .NET 7 時,有一些重大變更需要特別注意。 如需詳細資訊,請參閱 Windows Forms 的重大變更

高 DPI 改善

已改善具有 PerMonitorV2 的高 DPI 轉譯:

  • 正確調整巢狀控制項。 例如,面板上的按鈕,而該面板位於索引標籤頁面上。

  • 針對執行 ApplicationHighDpiMode 集合至 PerMonitorV2 的應用程式,根據其目前的監視器 DPI 設定,縮放 Form.MaximumSizeForm.MinimumSize 屬性。

    在 .NET 7 中,此功能預設為停用,您必須加入宣告才能接收此變更。 從 .NET 8 開始,此功能預設為啟用,您必須退出宣告才能還原為先前的行為。

    若要啟用功能,請在 runtimeconfig.json 中設定 configProperties 設定:

    {
      "runtimeOptions": {
        "tfm": "net7.0",
        "frameworks": [
            ...
        ],
        "configProperties": {
          "System.Windows.Forms.ScaleTopLevelFormMinMaxSizeForDpi": true,
        }
      }
    }
    

協助工具改善和修正

此版本進一步改善協助工具,包括但不限於下列項目:

資料繫結改善 (預覽)

雖然 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 已修正表單的貼齊版面配置。

另請參閱