Maui项目PackageVersion多值错误求助及环境问题排查
问题描述
- 加载MAUI解决方案时触发错误:
无法读取MauiApp的项目信息:PackageVersion属性在所有目标框架中应保持单一值,但当前存在1.0.0、2.0两个值 - 该应用8个月前可正常构建并发布至Google Play,现在打开VS2022准备更新时,所有NuGet包显示缺失(依赖项带警告标识)
- 已验证本机可创建并构建.NET 8的新MAUI项目,曾仅安装.NET 9 SDK,后补充安装.NET 8 SDK但问题未解决
- 不确定8个月前使用的.NET 8 SDK具体版本,怀疑依赖项查找路径错误
项目csproj文件
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks> <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET --> <!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> --> <!-- Note for MacCatalyst: The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64. When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>. The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated; either BOTH runtimes must be indicated or ONLY macatalyst-x64. --> <!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> --> <OutputType>Exe</OutputType> <RootNamespace>mauiapp</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <!-- Display name --> <ApplicationTitle>mauiapp</ApplicationTitle> <!-- App Identifier --> <ApplicationId>com.companyname.mauiapp</ApplicationId> <!-- Versions --> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> <ApplicationVersion>1</ApplicationVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</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.17763.0</TargetPlatformMinVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <AndroidKeyStore>False</AndroidKeyStore> <RunAOTCompilation>False</RunAOTCompilation> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-maccatalyst|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-windows10.0.19041.0|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-android|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <AndroidKeyStore>False</AndroidKeyStore> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-ios|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-maccatalyst|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-windows10.0.19041.0|AnyCPU'"> <ApplicationTitle>appstorename</ApplicationTitle> <ApplicationId>com.companynameinc.appstorename</ApplicationId> <ApplicationVersion>2</ApplicationVersion> <ApplicationDisplayVersion>2.0</ApplicationDisplayVersion> </PropertyGroup> <ItemGroup> <!-- App Icon --> <!-- Splash Screen --> <MauiSplashScreen Include="Resources\AppIcon\appiconfg.svg" /> <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" /> <!-- Images --> <MauiImage Include="Resources\Images\*" /> <MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" /> <!-- Custom Fonts --> <MauiFont Include="Resources\Fonts\*" /> <!-- Raw Assets (also remove the "Resources\Raw" prefix) --> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> </ItemGroup> <ItemGroup> <None Remove="Resources\AppIcon\appicon.svg" /> <None Remove="Resources\AppIcon\appiconfg.svg" /> <None Remove="signed_bundles\com.companynameinc.appstorename.aab" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" /> </ItemGroup> <ItemGroup> <Folder Include="signed_bundles\" /> </ItemGroup> <ItemGroup> <MauiIcon Include="Resources\AppIcon\appicon.svg" /> </ItemGroup> <ItemGroup> <Compile Update="Properties\Resources.Designer.cs"> <DesignTime>True</DesignTime> <AutoGen>True</AutoGen> <DependentUpon>Resources.resx</DependentUpon> </Compile> </ItemGroup> <ItemGroup> <EmbeddedResource Update="Properties\Resources.resx"> <Generator>ResXFileCodeGenerator</Generator> <LastGenOutput>Resources.Designer.cs</LastGenOutput> </EmbeddedResource> </ItemGroup> </Project>
解决方案与排查思路
一、先解决PackageVersion冲突错误
从csproj文件可见,全局PropertyGroup设置了ApplicationDisplayVersion=1.0和ApplicationVersion=1,但每个目标框架的Debug/Release配置又单独设置了ApplicationDisplayVersion=2.0和ApplicationVersion=2,导致版本值不一致触发错误。
解决方法:
- 若统一使用新版本(2.0):删除全局
PropertyGroup里的版本配置,或把全局版本值改成2.0和2,确保所有配置版本统一; - 若需不同配置用不同版本:确保每个目标框架的所有配置(Debug/Release)版本值一致,避免部分框架用1.0、部分用2.0的情况。
二、修复NuGet包缺失问题
1. 指定项目的.NET SDK版本
避免.NET 9 SDK干扰,可在项目根目录创建global.json文件,内容如下:
{ "sdk": { "version": "8.0.400", "rollForward": "LatestPatch" } }
该文件会强制项目使用指定的.NET 8 SDK版本,不会自动切换到.NET 9。
2. 清理NuGet缓存并重新还原
- 打开VS2022的NuGet包管理器,点击“还原”按钮;
- 或在项目根目录打开命令行,依次运行:
dotnet clean dotnet nuget locals all --clear dotnet restore
3. 明确MAUI包版本
项目中用$(MauiVersion)引用MAUI包,若SDK版本不一致可能导致解析失败,可直接指定兼容.NET 8的稳定版本,例如:
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.21" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.21" />
4. 验证目标框架与SDK匹配
确认项目TargetFrameworks均为net8.0开头,无版本混用;同时在VS2022项目属性的“应用”选项卡中,检查目标框架是否正确设置为.NET 8。
三、排查路径相关问题
- 检查VS2022工具选项中的NuGet包源配置,确保包含官方NuGet源;
- 确认项目根目录的
NuGet.Config(若存在)源配置正确,无访问限制; - 尝试将项目复制到无特殊字符、权限正常的新路径,排除原路径问题。
内容的提问来源于stack exchange,提问作者erotavlas
相关产品推荐
相关产品推荐

