Peringatan Kompilator WFAC010

Versi diperkenalkan: .NET 6

Hapus pengaturan DPI tinggi dari app.manifest dan konfigurasikan melalui API Application.SetHighDpiMode atau properti proyek ApplicationHighDpiMode.

Aplikasi Windows Forms harus menentukan kesadaran DPI aplikasi melalui konfigurasi aplikasi atau dengan API Application.SetHighDpiMode.

Penting

Dimulai dengan .NET 9, peringatan ini telah berubah menjadi WFO0003.

Penyelesaian masalah

Menggunakan C#

Gunakan API bootstrap baru dengan memanggil ApplicationConfiguration.Initialize metode sebelum Application.Run().

class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}

Metode ApplicationConfiguration.Initialize ini dihasilkan saat aplikasi Anda dikompilasi, berdasarkan pengaturan dalam file proyek aplikasi. Misalnya, lihat pengaturan berikut <Application*> :

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.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>

Pengaturan ini menghasilkan metode berikut:

[CompilerGenerated]
internal static partial class ApplicationConfiguration
{
    public static void Initialize()
    {
        global::System.Windows.Forms.Application.EnableVisualStyles();
        global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
        global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
        global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)3));
    }
}

Menggunakan Visual Basic

Visual Basic beroperasi sedikit berbeda dari C# saat ini. Pengaturan file proyek diperlukan agar Visual Studio mendeteksi pengaturan aplikasi, tetapi Anda juga harus mengonfigurasi pengaturan di halaman properti proyek Application>Framework (yang memengaruhi file My Project\Application.myapp) atau dalam peristiwa pengaktifan aplikasi.

Penting

Font tidak dapat diatur di pengaturan proyek.

Contoh kode berikut menunjukkan penanganan event ApplyApplicationDefaults untuk mengatur font default dan mode HighDPI:

Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
    Partial Friend Class MyApplication
        Private Sub MyApplication_ApplyApplicationDefaults(sender As Object, e As ApplyApplicationDefaultsEventArgs) Handles Me.ApplyApplicationDefaults
            e.Font = New Font("Microsoft Sans Serif", 8.25)
            e.HighDpiMode = HighDpiMode.SystemAware
        End Sub
    End Class
End Namespace

Mengelola peringatan

Tekan peringatan dengan salah satu metode berikut:

  • Atur tingkat keparahan aturan dalam file .editorConfig .

    [*.{cs,vb}]
    dotnet_diagnostic.WFAC010.severity = none
    

    Untuk informasi selengkapnya tentang file konfigurasi editor, lihat File konfigurasi untuk aturan analisis kode.

  • Tambahkan PropertyGroup berikut ke file proyek Anda:

    <PropertyGroup>
        <NoWarn>$(NoWarn);WFAC010</NoWarn>
    </PropertyGroup>
    
  • Batasi dalam kode dengan direktif #pragma warning disable WFAC010.