使用VS2022预览版与.NET7时找不到Microsoft.Maui.Controls.Maps NuGet包,如何安装?
解决.NET 7 MAUI中找不到Microsoft.Maui.Controls.Maps包的问题
核心说明
在.NET 7的MAUI项目里,Microsoft.Maui.Controls.Maps不再是独立的NuGet包——它已经整合进Microsoft.Maui.Controls主包及平台配套SDK中,无需单独搜索安装。
具体操作步骤
1. 确认核心包引用
检查你的项目文件(.csproj),确保已包含以下基础包:
<ItemGroup> <PackageReference Include="Microsoft.Maui.Controls" Version="7.0.0" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="7.0.0" /> </ItemGroup>
如果缺失,直接在项目文件中添加,或通过NuGet管理器搜索安装Microsoft.Maui.Controls(选择7.x.x版本,匹配.NET 7)。
2. 配置平台特定Map支持
不同平台需额外配置才能启用Map功能:
- Android:
- 在
Platforms/Android/AndroidManifest.xml添加权限:<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-feature android:name="android.hardware.location.gps" android:required="false" /> - 在
Platforms/Android/MainApplication.cs的CreateMauiApp中注册Map服务:builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler(typeof(Microsoft.Maui.Controls.Maps.Map), typeof(Microsoft.Maui.Controls.Compatibility.Platform.Android.MapHandler)); });
- 在
- iOS/Mac Catalyst:
- 在对应平台的
Info.plist中添加位置权限描述:<key>NSLocationWhenInUseUsageDescription</key> <string>需要访问您的位置以显示地图</string> - 在
AppDelegate.cs的CreateMauiApp中注册Map服务:builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler(typeof(Microsoft.Maui.Controls.Maps.Map), typeof(Microsoft.Maui.Controls.Compatibility.Platform.iOS.MapHandler)); });
- 在对应平台的
- Windows:无需额外包,确保项目目标框架为
net7.0-windows10.0.19041.0及以上,平台会自动集成Map支持。
3. 验证Map控件可用性
在XAML中直接引用使用:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps" x:Class="YourProjectNamespace.MainPage"> <maps:Map /> </ContentPage>
常见排查点
- 确认Visual Studio 2022 Preview 17.4.0 Preview 3已安装完整的MAUI工作负载。
- 清理并重建解决方案,避免NuGet缓存导致的引用异常。
- 检查项目目标框架:必须是
net7.0-android、net7.0-ios等MAUI多目标框架,纯net7.0类库/控制台项目不支持MAUI控件。
内容的提问来源于stack exchange,提问作者Ali Alfaraj
相关产品推荐
相关产品推荐

