You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET MAUI地图控件中设置深色/浅色模式?MapType.StreetNight不存在

.NET MAUI 地图控件深色/浅色模式设置方案

.NET MAUI 的 Map 控件本身没有直接提供类似 MapType.StreetNight 的枚举选项,但可以通过平台特定代码实现深色/浅色模式的切换,以下是各平台的具体实现方式:

Android 平台

Android 端的 Google Maps 可通过自定义地图样式文件实现深色模式:

  1. 在 Android 项目的 Resources/raw 文件夹中创建 JSON 样式文件(如 map_style_dark.json),可使用 Google 官方地图样式编辑器生成深色模式对应的 JSON 代码,示例片段:
[
  {
    "elementType": "geometry",
    "stylers": [
      { "color": "#242f3e" }
    ]
  },
  {
    "elementType": "labels.text.fill",
    "stylers": [
      { "color": "#746855" }
    ]
  }
]
  1. 在 MAUI 页面中通过 Handler 获取原生 GoogleMap 实例,应用自定义样式:
#if ANDROID
using Android.Gms.Maps;
using Microsoft.Maui.Maps.Handlers;

namespace YourAppNamespace;

public partial class MapPage : ContentPage
{
    protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();
        if (Handler is MapHandler mapHandler)
        {
            var googleMap = mapHandler.PlatformView;
            var darkStyle = MapStyle.LoadRawResourceStyle(Android.App.Application.Context, Resource.Raw.map_style_dark);
            googleMap.SetMapStyle(darkStyle);
        }
    }
}
#endif

iOS 平台

iOS 端的 MKMapView 支持直接通过地图类型切换实现深色模式:

  1. 单个地图页面针对性设置:
#if IOS
using MapKit;
using Microsoft.Maui.Maps.Handlers;

namespace YourAppNamespace;

public partial class MapPage : ContentPage
{
    protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();
        if (Handler is MapHandler mapHandler)
        {
            var mkMapView = mapHandler.PlatformView;
            mkMapView.MapType = Application.Current.RequestedTheme == AppTheme.Dark 
                ? MKMapType.DarkStandard 
                : MKMapType.Standard;
        }
    }
}
#endif
  1. 全局应用级设置(在 App.xaml.cs 中):
#if IOS
using MapKit;

namespace YourAppNamespace;

public partial class App : Application
{
    protected override Window CreateWindow(IActivationState activationState)
    {
        var window = base.CreateWindow(activationState);
        var mapAppearance = new MKMapAppearance();
        mapAppearance.SetDarkModeMapType(MKMapType.DarkStandard);
        MKMapView.Appearance = mapAppearance;
        return window;
    }
}
#endif

跟随系统主题自动切换

若需要跟随系统的深色/浅色主题自动切换地图样式,可监听主题变更事件并更新:

public partial class MapPage : ContentPage
{
    public MapPage()
    {
        InitializeComponent();
        Application.Current.RequestedThemeChanged += OnThemeChanged;
    }

    private void OnThemeChanged(object sender, AppThemeChangedEventArgs e)
    {
        UpdateMapTheme();
    }

    private void UpdateMapTheme()
    {
#if ANDROID
        if (Handler is MapHandler mapHandler)
        {
            var googleMap = mapHandler.PlatformView;
            var styleRes = Application.Current.RequestedTheme == AppTheme.Dark 
                ? Resource.Raw.map_style_dark 
                : Resource.Raw.map_style_light;
            var mapStyle = MapStyle.LoadRawResourceStyle(Android.App.Application.Context, styleRes);
            googleMap.SetMapStyle(mapStyle);
        }
#elif IOS
        if (Handler is MapHandler mapHandler)
        {
            var mkMapView = mapHandler.PlatformView;
            mkMapView.MapType = Application.Current.RequestedTheme == AppTheme.Dark 
                ? MKMapType.DarkStandard 
                : MKMapType.Standard;
        }
#endif
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        Application.Current.RequestedThemeChanged -= OnThemeChanged;
    }
}

内容的提问来源于stack exchange,提问作者Denis De Pauli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:22:45