我们知道 MAUI 是开发跨平台应用的解决方案 ,用 C# 可以直接把 iOS , Android , Windows , macOS , Linux ,Tizen 等应用开发出来。那我们在这个框架除了用底层自定义的 UI 控件外,如果我们要用如高德地图这样的第三方控件,要如何做呢?接下来我就和大家介绍一下。
如果你还没有学习原生库绑定的知识,可以到以下链接学习相关内容:
关于做一个高德地图的 iOS / Android MAUI 控件(iOS 原生库绑定)
https://github.com/kinfey/AMapMAUIControls/blob/main/tutorial/cn/01.iOSBinding.md
做一个高德地图的 iOS / Android MAUI 控件(Android 原生库绑定)
https://github.com/kinfey/AMapMAUIControls/blob/main/tutorial/cn/02.DroidBinding.md
在上面两个例子中我们学习到把原生高德的 iOS / Android SDK 绑定,也用 .NET for iOS 和 .NET for Android 进行了调用。但要用 MAUI 就意味着调用方式改变, 一次性编写多平台使用。要实现这个效果,先看看 MAUI 的基础架构。
我们可以清楚看到, MAUI 除了公用的 xmal 文件外, 实际上也把特定平台的一些设定放置到 Platforms 的文件夹内, Platforms 的自文件夹就是对应的平台。 我们知道可以根据不同平台去渲染平台界面。这就是我们常说的自定义平台控件了。
在前言部分我们也提到了 MAUI 采用 Handler 模式去设定平台界面。如果我们要实现一个高德地图的 MAUI 控件,具体的架构是这样的
我们需要去创建如上图的一个结构,我们需要为 AMap 添加一个共享文件 AMap.shared.cs ,这个文件 AMapHandler 继自 ViewHandler。
public interface IAMap : IView
{
}
public class AMap : View, IAMap
{
}
partial class AMapHandler
{
public static IPropertyMapper<AMap, AM