为什么在NET6.0-WINDOWS项目中会未定义NET6_0_WINDOWS



我有一个遗留程序,正在尝试从Net Framework 4.6.1迁移到.Net 6。在它中,有一个共享库,需要在Linux和Windows上运行,其中有一些特定于Windows的调用,我已经使用#if NET6_0_Windows成功隔离了这些调用。这足以让Linux版本启动并运行,但当我尝试添加Windows WPF应用程序时,我遇到了数千个错误。为了更新WPF应用程序,我运行了升级助手(此处详细介绍(。不幸的是,如果不付出相当大的努力,该应用程序就无法编译。

我在UI中将我的项目文件设置为显式地以窗口为目标(使用net6.0-windows作为TargetFramework(,并且我的xaml文件不会与代码关联(这是一个常见的潜在原因LOTS问题,我正在使用这里的线程单独调查。例如,我在InitializeComponent上遇到编译器找不到的错误,以及线程中提到的错误。

更奇怪的是,当我运行该应用程序时(经过#if NET6_0_WINDOWS的多次调整(,我无法在#if’d块中的代码中设置断点。。。我可能正沿着一条兔子的轨迹走下去,但为什么那个符号没有定义呢?

在相关的情况下,正在运行的WPF应用程序的项目文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<OutputType>WinExe</OutputType>
<RootNamespace>WPFApp</RootNamespace>
<ApplicationIcon>SoftingIcon.ico</ApplicationIcon>
<PublishUrl>publish</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>..binDebug</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<OutputPath>..binDebug</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>..binRelease</OutputPath>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<OutputPath>..binRelease</OutputPath>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
<ItemGroup>
<Reference Update="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Update="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Update="UIAutomationProvider">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<AppDesigner Include="Properties" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="....commonEditModelEditModel.csproj" />
<ProjectReference Include="....commonToolsTools.csproj" />
<ProjectReference Include="..HelpHelpInterfaceHelpInterface.csproj" />
<ProjectReference Include="..ViewPaneLibraryViewPaneLibrary.csproj" />
<ProjectReference Include="..InterfaceControlsInterfaceControls.csproj" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">
<Visible>False</Visible>
<ProductName>.NET Framework 2.0 %28x86%29</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.0">
<Visible>False</Visible>
<ProductName>.NET Framework 3.0 %28x86%29</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</ItemGroup>
</Project>

预处理器符号只包括.Net版本,而不包括操作系统(也要小心,Net和6之间没有下划线(。

符号的完整列表可在此处找到:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

但是,您可以在MSBuild中自己定义它。

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
<DefineConstants>$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
</PropertyGroup>

最新更新