【问题标题】:How can I apply a style or change colors in map using Xamarin.Forms.Maps (No Xamarin.Forms.GoogleMaps)如何使用 Xamarin.Forms.Maps(无 Xamarin.Forms.GoogleMaps)在地图中应用样式或更改颜色
【发布时间】:2021-09-10 00:12:44
【问题描述】:

正如我在标题中所写,我使用 Microsoft Xamarin.Forms.Maps 块在我的应用程序中显示地图。 首先我从 Xamarin.Forms.GoogleMaps 开始,但是当我看到无法自定义 InfoWindows(当我触摸一个引脚时)时,我必须返回并开始使用 Xamarin.Forms.Maps。 唯一的问题是我必须将“灰色样式”应用于地图或类似的东西。而且我找不到任何链接或指南来执行此操作。 CustomRender 似乎无法修改样式或颜色。

请问,有人可以帮我吗?任何链接、文档或示例?

【问题讨论】:

  • “灰色风格”是什么意思?

标签: xamarin.forms custom-renderer xamarin.forms.maps


【解决方案1】:

如果您指的是Dark Mode,您可以创建自定义渲染器来实现。

iOS

OnElementChanged 中设置OverrideUserInterfaceStyle

[assembly: ExportRenderer(typeof(Map), typeof(MyRenderer))]
namespace FormsApp.iOS
{
    class MyRenderer : MapRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                var nativeMap = Control as MKMapView;
                nativeMap.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Dark;
            }
        }
    }
}

安卓

在资源中创建暗模式文件并在OnElementChanged 中设置mapStyle

[assembly: ExportRenderer(typeof(Map), typeof(MyMapRenderer))]
namespace FormsApp.Droid
{
    class MyMapRenderer : MapRenderer
    {
        Context _context;

        public MyMapRenderer(Context context) : base(context)
        {
            _context = context;        
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if(e.NewElement != null)
            {
                NativeMap.SetMapStyle(MapStyleOptions.LoadRawResourceStyle(this.Context, Resource.Raw.map_style_night));
            }
        }
    }
}

参考

https://forums.xamarin.com/discussion/comment/429548/#Comment_429548.

How to use Dark Mode Google Maps in Xamarin.Forms.Maps?.

【讨论】:

  • 我指的是这样的例子:mapstyle.withgoogle.com.
  • 例如,如果我在这个网站 (mapstyle.withgoogle.com) 中创建了我的风格地图,我能否使用您显示的自定义渲染设置地图风格?
  • 我认为您的参考链接谈论 Xamarin.Forms.GoogleMap,我使用 Xamarin.Forms.Maps。有没有什么办法可以用这个金块打造个人风格?
  • 不,这是 Xamarin.Forms.Maps 的解决方案,点击该链接了解详情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2021-08-24
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
相关资源
最近更新 更多