Waarschuwing voor compiler WFO0003

versie geïntroduceerd: .NET 9

Verwijder hoge DPI-instellingen uit app.manifest en configureer deze via Application.SetHighDpiMode API of ApplicationHighDpiMode projecteigenschap.

Windows Forms-toepassingen moeten applicatie-DPI-ondersteuning opgeven via de applicatieconfiguratie of met de Application.SetHighDpiMode API.

Voorbeeld

Uw project bevat een app.manifest bestand met de instelling <dpiAware>:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <!-- other settings omitted for brevity -->
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
</assembly>

Deze waarschuwing corrigeren

Windows Forms-toepassingen moeten applicatie-DPI-ondersteuning opgeven via de applicatieconfiguratie of met de Application.SetHighDpiMode API.

C# gebruiken

Roep de methode ApplicationConfiguration.Initialize aan voordat Application.Run().

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

De ApplicationConfiguration.Initialize methode wordt gegenereerd wanneer uw app wordt gecompileerd, op basis van de instellingen in het projectbestand van de app. Bekijk bijvoorbeeld de volgende <Application*> instellingen:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>

    <ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
    <ApplicationVisualStyles>true</ApplicationVisualStyles>
    <ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
    <ApplicationDefaultFont>Microsoft Sans Serif, 8.25pt</ApplicationDefaultFont>

  </PropertyGroup>

</Project>

Met deze instellingen wordt de volgende methode gegenereerd:

[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));
    }
}

Visual Basic gebruiken

Visual Basic stelt de high DPI-modus op twee plaatsen in. De eerste bevindt zich in de projecteigenschappen, die van invloed zijn op Visual Studio Designer, maar niet van invloed zijn op de app tijdens runtime.

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <StartupObject>Sub Main</StartupObject>
    <UseWindowsForms>true</UseWindowsForms>
    <MyType>WindowsForms</MyType>

    <ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
    <ApplicationVisualStyles>false</ApplicationVisualStyles>
    <ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
    <ApplicationDefaultFont>Microsoft Sans Serif, 8.25pt</ApplicationDefaultFont>

  </PropertyGroup>

</Project>

De tweede plaats vindt u in de Visual Basic Application Framework-instellingen. Deze bevinden zich op de pagina met projecteigenschappen, onder Application>Application Framework. Deze instellingen worden opgeslagen in het bestand Mijn project\Application.myapp of in een opstartgebeurtenishandler van een toepassing.

Het lettertype instellen in Visual Basic

Het runtimelettertype kan echter niet worden geconfigureerd in Visual Basic via een app-instelling of het projectbestand. Dit moet worden ingesteld in de ApplyApplicationDefaults event handler.

Imports Microsoft.VisualBasic.ApplicationServices

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

De waarschuwing beheren

Onderdrukt de waarschuwing met een van de volgende methoden:

  • Stel de ernst van de regel in het bestand .editorConfig.

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

    Zie Configuratiebestanden voor codeanalyseregelsvoor meer informatie over editorconfiguratiebestanden.

  • Voeg de volgende PropertyGroup toe aan uw projectbestand:

    <PropertyGroup>
        <NoWarn>$(NoWarn);WFO0003</NoWarn>
    </PropertyGroup>
    
  • Onderdrukken in code met de #pragma warning disable WFO0003 richtlijn.