MoveToRegion在升级到API25后无法工作

MoveToRegion在升级到API25后无法工作

本文介绍了Xamarin表单映射Android CustomRenderer MoveToRegion在升级到API25后无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的应用程序升级到了Android 7.1(API25),并更新了NuGet Packages,如下所述:

I've just upgraded my application to Android 7.1(API25) and update NuGet Packages as decribed below:

Xamarin.GooglePlayServive.Maps to v42.1021.1
Xamarin.Forms to v2.3.4.247
Xamarin.Forms.Maps to v2.3.4.247
Xamarin.Android.Support.V4 to v25.3.1
Xamarin.Android.Support.V7.* to v25.3.1

我受益从一个派生自Map类的CustomMap类和一个android CustomRenderer。一切都很好,但更新之后,MoveToRegion()方法不会做任何事情,甚至map也不会初始化我传递给ctor的初始MapSpan。

I'm benefiting from a CustomMap class derived from Map class and an android CustomRenderer. Everything was working well before, but after update, the method MoveToRegion() doesn't do anything and even map doesn't initialize at the initial MapSpan I passed to ctor.

我找不到原因。
请给我任何可能的原因。我会尽力解决这个FORCE MAJOR问题!

I can't find out the reason.Please give me any possible cause. I'll try anything to solve this FORCE MAJOR Issue!

推荐答案

现在我发现了一个解决这个问题的方法。

I found a hack to overcome this issue for now.https://forums.xamarin.com/discussion/comment/264930/#Comment_264930

我的CustomMapRenderer类(Xamarin.Forms.Maps.Android.MapRenderer)的基类有一个实现IOnMapReadyCallback的方法, OnMapReady没有被调用。我所要做的就是实现IOnMapReadyCallback并添加一个方法OnMapReady,它调用InvokeOnMapReadyBaseClassHack(调用基类OnMapReady):

It appears that the base class of my CustomMapRenderer class(Xamarin.Forms.Maps.Android.MapRenderer) has a method for implementing IOnMapReadyCallback named OnMapReady which doesn't get called. All I had to do was implementing IOnMapReadyCallback and add a method OnMapReady that calls InvokeOnMapReadyBaseClassHack(Calling base class OnMapReady):

public void OnMapReady(GoogleMap googleMap)
{
    ...

    InvokeOnMapReadyBaseClassHack(googleMap);
}

private void InvokeOnMapReadyBaseClassHack(GoogleMap googleMap)
{
    System.Reflection.MethodInfo onMapReadyMethodInfo = null;

    Type baseType = typeof(MapRenderer);
    foreach (var currentMethod in baseType.GetMethods(System.Reflection.BindingFlags.NonPublic |
                                                        System.Reflection.BindingFlags.Instance |
                                                        System.Reflection.BindingFlags.DeclaredOnly))
    {

        if (currentMethod.IsFinal && currentMethod.IsPrivate)
        {
            if (string.Equals(currentMethod.Name, "OnMapReady", StringComparison.Ordinal))
            {
                onMapReadyMethodInfo = currentMethod;

                break;
            }

            if (currentMethod.Name.EndsWith(".OnMapReady", StringComparison.Ordinal))
            {
                onMapReadyMethodInfo = currentMethod;

                break;
            }
        }
    }

    if (onMapReadyMethodInfo != null)
    {
        onMapReadyMethodInfo.Invoke(this, new[] { googleMap });
    }
}

这篇关于Xamarin表单映射Android CustomRenderer MoveToRegion在升级到API25后无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:30