Xamarin.Forms.Maps 2.3.4自定义MapRenderer禁用所有内容 [英] Xamarin.Forms.Maps 2.3.4 custom MapRenderer disables everything

查看:231
本文介绍了Xamarin.Forms.Maps 2.3.4自定义MapRenderer禁用所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我更新Xamarin.Forms和Xamarin.Forms.Maps到新版本(2.3.4)之后出现了我的问题。

之后,我也更新了所有google在Android项目中播放服务(以及很多我讨厌的库)。

主要问题是我有一个自定义的MapRenderer用于定制引脚,在iOS和UWP工程中很好,但是在Android版本中,这个自定义的MapRenderer分散了所有的Map。任何属性更改或方法调用似乎都被忽略。例如,我有一个用于切换地图类型(混合或街道)的按钮,并且该操作不会更改它。我还注意到(根据本教程: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/customized-pin/ )属性VisibleRegion永不改变,所以下面的代码永远不会执行:



pre $ protected override void OnElementPropertyChanged(object sender,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, E);

if(e.PropertyName.Equals(VisibleRegion)&&!_isDrawn)
{
//对您的自定义地图
做些什么

$ b

如果内 i用于填充我的自定义引脚(如上面的教程),现在我的地图总是空的。



现在我在 IOnMapReadyCallback ,它工作正常,但我仍然有绑定问题。

如果我忽略自定义MapRendered(删除装配线)所有绑定开始工作正常,但我的地图现在有旧的针脚,并且所有的定制都消失了(显然)。

在PCL中,我有类似于 MyMap的东西。 MoveToRegion(...) MyMap.MapType = _currentType; 但这些指令仅适用于不使用自定义MapRenderer的情况。 p>

我的自定义MapRenderer与上面的教程几乎一样。



自定义Map是cre使用C#而不使用XAML,它没有任何XAML绑定,但是如果我使用MapRenderer,则完全忽略像MoveToRegion或MapType这样的任何属性更改或方法调用。



任何帮助?



谢谢 找到了解决方案。



查看源代码,MapRenderer已经实现了 IOnMapReadyCallback ,如果您删除了自定义MapRendered,一切都开始工作(但没有定制)。 MapRenderer将Google地图实例保存在属性 NativeMap 中(也存在属性 Map 即Xamarin表格地图实例),所以我们不需要再实现 IOnMapReadyCallback 。我认为在使用 NativeMap 时需要小心,因为在开始时它可能是 null



在我之前提到的方法中,现在我这样做:

  protected override void OnElementPropertyChanged(object sender,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender,e);

if(e.PropertyName.Equals(VisibleRegion)&&!_isDrawn)
{
PopulateMap();
OnGoogleMapReady();
}
}

现在OnMapReady中的代码进入 OnGoogleMapReady()
$ b

  private void OnGoogleMapReady()
{
if(_mapReady)return;

NativeMap.InfoWindowClick + = OnInfoWindowClick;
NativeMap.SetInfoWindowAdapter(this);

_mapReady = true;
}

我也在 OnElementChanged 删除任何注册的委托在 NativeMap

  if(e .OldElement!= null)
{
NativeMap.InfoWindowClick = - OnInfoWindowClick;
}

目前存在 Pull Request ,它将实现OnMapReady作为虚拟方法,所以我们可以在我们的实现中覆盖它,现在确保NativeMap不为null,但为此我们需要等待下一个版本。

您可以在这里阅读更多 - > https://forums.xamarin.com/discussion/92565/android-ionmapreadycallback-forms-2-3-4


My problem occurs after I updated Xamarin.Forms and Xamarin.Forms.Maps to the new version (2.3.4).

After that I also updated all google play services in Android project (and a lot of libraries that I hate).

The main problem is that I have a custom MapRenderer for custom pins, in iOS and UWP works fine, but in Android version this custom MapRenderer brokes all the Map. Any property change or method call seems to be ignored.

For example I have a button to toggle the map type (Hybrid or Street) and that action never changes it. I also noticed (according this tutorial: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/customized-pin/) that the property "VisibleRegion" never changes so the following code never executes:

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName.Equals("VisibleRegion") && !_isDrawn)
        {
            // Do something with your custom map
        }
    }

Inside that if i used to populate my custom pins (like the tutorial above) and now my Map is always empty.

Now i populate my map inside the IOnMapReadyCallback and it works fine, but the I still have the bindings problem.

If I ignore the custom MapRendered (removing the assembly line) all the bindings starts working fine but my map now have the old pins and all customization is gone (obviously).

In the PCL I have things like MyMap.MoveToRegion(...) and MyMap.MapType = _currentType; but those instructions only works if a don't use a custom MapRenderer.

My custom MapRenderer is almost the same as the tutorial above.

The custom Map is created with C# and not with XAML, it doesn't have any XAML binding but any property change or method call like the MoveToRegion or MapType is totally ignored if i'm using the MapRenderer.

Any help?

Thanks

解决方案

I already found the solution.

Looking at the source code, MapRenderer already implements IOnMapReadyCallback and if you remove the implementation in the custom MapRendered, everything starts working again (but with no customization).

MapRenderer saves the google map instance in the property NativeMap (also exists the property Map that is the Xamarin forms map instance) so we don't need to implement IOnMapReadyCallback any more. I think we need to be careful in the use of NativeMap because at the begining it could be null.

In the method I mentioned before now i do this:

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName.Equals("VisibleRegion") && !_isDrawn)
        {
            PopulateMap();
            OnGoogleMapReady();
        }
    }

and the code I had in OnMapReady now goes inside OnGoogleMapReady():

    private void OnGoogleMapReady()
    {
        if (_mapReady) return;

        NativeMap.InfoWindowClick += OnInfoWindowClick;
        NativeMap.SetInfoWindowAdapter(this);

        _mapReady = true;
    }

I also added this in OnElementChanged to remove any registered delegate in NativeMap

        if (e.OldElement != null)
        {
            NativeMap.InfoWindowClick -= OnInfoWindowClick;
        }

At the moment exists a Pull Request that implements OnMapReady as virtual method, so we can override it in our implementation and now be sure when NativeMap is not null, but for that we need to wait for a next release.

You can read more here -> https://forums.xamarin.com/discussion/92565/android-ionmapreadycallback-forms-2-3-4

这篇关于Xamarin.Forms.Maps 2.3.4自定义MapRenderer禁用所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆