使用SupportMapFragment代替MapFragment [英] Using SupportMapFragment instead of MapFragment

查看:1162
本文介绍了使用SupportMapFragment代替MapFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xamarin,我已经修改了谷歌地图API 2样品使用 SupportMapFragment 对象,而不是 MapFragment 对象。

I am using Xamarin and I have modified the Google Maps API 2 sample to use SupportMapFragment objects rather than MapFragment objects.

我能有一定的帮助与 InitMapFragment 功能。

Can I have some help with the InitMapFragment function.

下面是code:

private void InitMapFragment()
{
    _mapFragment = FragmentManager.FindFragmentByTag("map") as SupportMapFragment;
    if (_mapFragment == null)
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
            .InvokeMapType(GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(true)
            .InvokeCompassEnabled(true);

        FragmentTransaction fragTx = FragmentManager.BeginTransaction();
        _mapFragment = SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "map");
        fragTx.Commit();
    }
}

_mapFragment 曾经是键入 MapFragment ,但现在是 SupportMapFragment

_mapFragment used to be of type MapFragment, but now is SupportMapFragment.

此外,该活动是由活动继承的那一刻,这应该是 FragmentActivity ,还是其他什么东西?

Also, the activity is inheriting from Activity at the moment, should this be FragmentActivity, or something else?

下面是我收到的错误:

错误CS0039:无法将类型'Android.App.Fragment来
  Android.Gms.Maps.SupportMapFragment'通过引用转换,
  装箱转换,取消装箱转换,包装转换或null
  类型转换

Error CS0039: Cannot convert type 'Android.App.Fragment' to 'Android.Gms.Maps.SupportMapFragment' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

错误CS1503:参数2:不能转换
  Android.Gms.Maps.SupportMapFragment'到'Android.App.Fragment

Error CS1503: Argument 2: cannot convert from 'Android.Gms.Maps.SupportMapFragment' to 'Android.App.Fragment'

我是pretty肯定,我需要使用 SupportFragmentManager 而非 FragmentManager ,但会像一些帮助,请。

I am pretty sure that I need to use a SupportFragmentManager rather than a FragmentManager, yet would like some help please.

修改

当尝试使用SupportFragmentManager,我收到以下错误:

When trying to use SupportFragmentManager, I am getting the following error:

错误CS0103:'SupportFragmentManager'这个名字并不在存在
  当前上下文

Error CS0103: The name 'SupportFragmentManager' does not exist in the current context

在此先感谢

推荐答案

SupportFramgentManager从FragmentActivity继承,以便确保活动延伸FragmentActivity。然后修改code如下:

SupportFramgentManager is inherited from FragmentActivity so make sure the activity extends FragmentActivity. Then modify your code as follows:

private void InitMapFragment()
{
    _mapFragment = SupportFragmentManager.FindFragmentByTag("map") as SupportMapFragment;
    if (_mapFragment == null)
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
            .InvokeMapType(GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(true)
            .InvokeCompassEnabled(true);

        FragmentTransaction fragTx = SupportFragmentManager.BeginTransaction();
        _mapFragment = SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "map");
        fragTx.Commit();
    }
}

这篇关于使用SupportMapFragment代替MapFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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