打破微软封印面向未来创建.NET Framework4.8工程

摘要:

  1. 工程从.NET Framework 4.8升级到.NET 8.0,即使采用官方方案也是很繁琐的一件事情,而且容易出问题。
  2. Windows 11内置了.NET Framework 4.8,所以当前的软件需要基于.NET Framework 4.8。但后续微软推出Windows 12,将内置更高版本的.NET版本,如何让软件跟随微软步伐快速平稳升级?
  3. 本文叫你如何打破微软封印,在VS2022中如何高屋建瓴面向未来优雅地创建.NET Framework 4.8工程。

关键工作流程:

  1. 创建新项目,【WPF 应用程序】
  1. 选择框架【.NET 8.0(长期支持)】
  1. 基于.NET 8.0的工程文件【HAPTICCAL.csproj】
xml 复制代码
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <RootNamespace>FirstSolver</RootNamespace>
    <ApplicationIcon>$(AssemblyName).ico</ApplicationIcon>
    <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
    <Authors>秦建辉</Authors>
    <AssemblyVersion>4.3.0</AssemblyVersion>
    <FileVersion>$(AssemblyVersion)</FileVersion>
    <NeutralLanguage>zh-Hans</NeutralLanguage>
    <StartupObject>FirstSolver.Program</StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="C2VColorEdit" Version="3.0.0" />
    <PackageReference Include="C2VLanguage" Version="3.0.0" />
    <PackageReference Include="HPSocket.Net" Version="5.9.3.1" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="NLogHelper" Version="3.0.0" />
    <PackageReference Include="NLogViewer" Version="3.0.0" />
    <PackageReference Include="TF_Communication_CS" Version="3.0.4" />
  </ItemGroup>

  <ItemGroup>
    <Resource Include="..\TF.png" Link="TF.png" />
    <Resource Include="$(AssemblyName).ico" />
  </ItemGroup>

  <ItemGroup>
    <None Update="Languages\en-US\$(AssemblyName).json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Languages\zh-Hans\$(AssemblyName).json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Languages\zh-Hant\$(AssemblyName).json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="$(AssemblyName)_Config.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="PowerShell.exe -ExecutionPolicy RemoteSigned -Command &quot;&amp;'$(ProjectDir)PostBuild.ps1' '$(TargetDir)' '$(ProjectPath)' '$(AssemblyName)'&quot;" />
  </Target>

</Project>
  1. 如图所示更改工程文件【HAPTICCAL.csproj】,重点:TargetFramework改成TargetFrameworks
  1. 新的工程文件【HAPTICCAL.csproj】
xml 复制代码
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFrameworks>net480</TargetFrameworks>
    <UseWPF>true</UseWPF>
    <RootNamespace>FirstSolver</RootNamespace>
    <ApplicationIcon>$(AssemblyName).ico</ApplicationIcon>
    <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
    <Authors>秦建辉</Authors>
    <AssemblyVersion>4.3.0</AssemblyVersion>
    <FileVersion>$(AssemblyVersion)</FileVersion>
    <NeutralLanguage>zh-Hans</NeutralLanguage>
    <StartupObject>FirstSolver.Program</StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="C2VColorEdit" Version="3.0.0" />
    <PackageReference Include="C2VLanguage" Version="3.0.0" />
    <PackageReference Include="HPSocket.Net" Version="5.9.3.1" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="NLogHelper" Version="3.0.0" />
    <PackageReference Include="NLogViewer" Version="3.0.0" />
    <PackageReference Include="TF_Communication_CS" Version="3.0.4" />
  </ItemGroup>

  <ItemGroup>
    <Resource Include="..\TF.png" Link="TF.png" />
    <Resource Include="$(AssemblyName).ico" />
  </ItemGroup>

  <ItemGroup>
    <None Update="Languages\en-US\$(AssemblyName).json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Languages\zh-Hans\$(AssemblyName).json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Languages\zh-Hant\$(AssemblyName).json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="$(AssemblyName)_Config.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="PowerShell.exe -ExecutionPolicy RemoteSigned -Command &quot;&amp;'$(ProjectDir)PostBuild.ps1' '$(TargetDir)' '$(ProjectPath)' '$(AssemblyName)'&quot;" />
  </Target>

</Project>
  1. 出现异常,然后【卸载项目】,再【重新加载项目】。
  2. 面向未来升级到.NET 8.0只需要修改"net480",将"net480"改为"net8.0-windows"即可。
相关推荐
C-cat.2 小时前
Linux|进程程序替换
linux·服务器·microsoft
zhy8103024 小时前
.net6 使用 FreeSpire.XLS 实现 excel 转 pdf - docker 部署
pdf·.net·excel
初九之潜龙勿用4 小时前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net
Smartdaili China4 小时前
如何在 Microsoft Edge 中设置代理: 快速而简单的方法
前端·爬虫·安全·microsoft·edge·社交·动态住宅代理
慧都小妮子5 小时前
Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
java·pdf·.net
霍先生的虚拟宇宙网络7 小时前
.net 支持跨平台(桌面)系列技术汇总
.net
djk88887 小时前
.net的winfrom程序 窗体透明&打开窗体时出现在屏幕右上角
.net
九鼎科技-Leo16 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
小白不太白95019 小时前
设计模式之 外观模式
microsoft·设计模式·外观模式
万里沧海寄云帆20 小时前
Word 插入分节符页码更新问题
windows·microsoft·word