Moniker du cadre cible
Familiarisons-nous. Dans .NET 5.0, pour utiliser Windows Forms ou WPF, il ne nous suffit pas de spécifier net5.0:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
Lorsque vous essayez d'utiliser Windows Forms ou WPF, nous obtenons l'erreur
C:\Program Files\dotnet\sdk\5.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(369,5): error NETSDK1136: The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so.
La solution, comme le suggère l'erreur, consiste à spécifier le nom du Framework cible
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
Comment ça fonctionne
La version importera automatiquement les fichiers de Microsoft.NET.Sdk \ cibles.
En outre, dotnet \ sdk \ 5.0 \ Sdks \ Microsoft.NET.Sdk.WindowsDesktop \ cibles \ Microsoft.NET.Sdk.WindowsDesktop.props contient le code:
<FrameworkReference Include="Microsoft.WindowsDesktop.App" IsImplicitlyDefined="true"
Condition="('$(UseWPF)' == 'true') And ('$(UseWindowsForms)' == 'true')"/>
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" IsImplicitlyDefined="true"
Condition="('$(UseWPF)' == 'true') And ('$(UseWindowsForms)' != 'true')"/>
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" IsImplicitlyDefined="true"
Condition="('$(UseWPF)' != 'true') And ('$(UseWindowsForms)' == 'true')"/>
Où est le problème
, FrameworkReference : .NET , NuGet
, - , Windows Forms WPF 'net5.0-windows'.
, .
Windows Forms WPF , 60 .
using System.Windows.Forms;
namespace Library
{
public class Demo
{
void ShowForm()
{
var f = new Form();
f.Show();
}
}
}
using System;
class Program
{
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
, Library.Demo.
dotnet publish:
dotnet publish ConsoleApp.csproj --self-contained -c Release -r win-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true /p:IncludeAllContentForSelfExtract=true
81,8!
IncludeAllContentForSelfExtract %TEMP%\.net .
?
Library.Demo, PublishTrimmed, Windows Forms .
dotnet publish , !
1
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<!-- -->
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
<ItemGroup>
<!-- .NET Runtime -->
<!-- PrivateAssets="all" , -->
<FrameworkReference Include="Microsoft.NETCore.App" />
<!-- Windows Desktop -->
<!-- PrivateAssets="all" - -->
<FrameworkReference Include="Microsoft.WindowsDesktop.App" PrivateAssets="all" />
<!-- :
Microsoft.WindowsDesktop.App.WPF
Microsoft.WindowsDesktop.App.WindowsForms -->
</ItemGroup>
</Project>
DisableImplicitFrameworkReference
2
.net5.0-windows .net5.0
:
dotnet publish ConsoleApp.csproj --self-contained -c Release -r win-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true /p:IncludeAllContentForSelfExtract=true
18.8
Devriez-vous faire cela dans les bibliothèques?
Définitivement oui!
D'une part, cela vous permet d'utiliser des types de Windows Forms ou WPF, d'autre part, le collecteur est capable de supprimer tous les inutilisés et de produire une taille de fichier plus petite.