.NET MAUI迁移项目Windows构建报错:project.assets.json缺失目标框架
.NET MAUI迁移项目在Windows Visual Studio构建时出现Assets文件目标缺失错误
错误信息
构建时触发以下系列错误:
Assets file '..obj\project.assets.json' doesn't have a target for 'net6.0-android'. Ensure that restore has run and that you have included 'net6.0-android' in the TargetFrameworks for your project. Assets file '..obj\project.assets.json' doesn't have a target for 'net6.0-windows10.0.22621.0'. Ensure that restore has run and that you have included 'net6.0-windows10.0.22621.0' in the TargetFrameworks for your project. Assets file '..obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. Assets file '..obj\project.assets.json' doesn't have a target for 'net6.0-ios'. Ensure that restore has run and that you have included 'net6.0-ios' in the TargetFrameworks for your project.
项目csproj核心配置
<PropertyGroup> <TargetFrameworks>net6.0-ios;net6.0-android;net6.0</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.22621.0</TargetFrameworks> <OutputType>Exe</OutputType> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion> <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.22621.0</TargetPlatformMinVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion> <DefaultLanguage>en</DefaultLanguage> </PropertyGroup> <PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'"> <OutputType>Library</OutputType> </PropertyGroup>
已尝试的无效操作
- 清理项目
- 删除obj和bin目录
- 重新安装.NET MAUI工作负载
问题根源与解决方法
问题原因
全局PropertyGroup中设置版本号的条件判断代码干扰了MSBuild的目标解析流程:
<ApplicationDisplayVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'ios'" >7.8.12844</ApplicationDisplayVersion> <ApplicationVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'ios'">2022092726</ApplicationVersion>
解决办法
将版本配置移至单独的、带有明确条件的PropertyGroup中,避免全局条件判断影响目标框架的恢复:
<PropertyGroup Condition="$(TargetFramework.Contains('-android')) and '$(Configuration)' == 'Release'"> <ApplicationDisplayVersion>7.8.12844</ApplicationDisplayVersion> <ApplicationVersion>2022092726</ApplicationVersion> </PropertyGroup>
修改后项目可在Windows Visual Studio中正常构建。
内容的提问来源于stack exchange,提问作者NPadrutt
相关产品推荐
相关产品推荐

