首页 > 解决方案 > .net 框架到 .net 5 迁移构建但未启动

问题描述

我刚刚将我的 .net Framework 4.8 项目迁移到了 .net 5 中。

首先,我将旧的 csproj 项目配置文件更改为新的。然后删除了多余的程序集,最后删除或更新了所有包。之后我启动了项目兼容性测试,看看我的所有项目是否都与 .net 5 兼容并启动该项目。

奇怪的是,没有更多细节,项目构建(我有一个 .exe),立即启动并关闭自己,我唯一的线索是这条消息:

The target process terminated without raising a CoreCLR start event. Make sure the target process is 
configured to use .NET Core. This can be expected behavior if the target process did not run on .NET Core.
Program 'test.exe' terminated with code -2147450740 (0x8000808c).
The program 'test.exe: Program trace' has stopped with code 0 (0x0).

我的 csproj 文件的一部分

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>    
     <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>

有人知道出了什么问题吗?

标签: c#.netwpfvisual-studio.net-5

解决方案


我通过以下步骤将我的名为FrameworkToNet5的demo项目(以framework4.7为目标)转换为.Net 5,并且成功,希望对您参考有所帮助:

步骤 1:在 Visual Studio 16.8.1 中创建一个名为FrameworkToNet5Microsoft.Xaml.Behaviors.Wpf的框架 WPF 项目,并安装在 Nuget 包中。

第 2 步:要启用设计器,请转到工具 > 选项 > 环境 > 预览功能,然后选择使用 .NET Core 应用程序的预览 Windows 窗体设计器选项。

第 3 步:右键单击FrameworkToNet5,然后Analyze and Code Cleanup单击Run Code Analysis以检查 .Net 5。

第 4 步:右键单击packages.config> Migrate packages.config to PackageReference,然后选择所有top-level包。

第 5 步:使用以下代码替换 .csproj

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

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.19" />
</ItemGroup>

第 6 步:使用以下代码替换 AssemblyInfo.cs 的内容

using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, 
ResourceDictionaryLocation.SourceAssembly )]

第 7 步:清理并重建项目。


推荐阅读