c# – 旋转设备后如何保存/重新绑定MapFragment上的事件侦听器(纵向/横向)?

前端之家收集整理的这篇文章主要介绍了c# – 旋转设备后如何保存/重新绑定MapFragment上的事件侦听器(纵向/横向)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Xamarin(7.1版)开发 Android应用程序.它显示为地图并绘制PolyLines,在OnCameraIdle()中执行此操作.

在OnCreate中以编程方式生成MapFragment.我通过GetMapAsync在OnResume中获取GoogleMap,并在OnMapReady中绑定侦听器.
他们工作正常,但只在一开始.一旦设备旋转(纵向 – >横向或反之亦然),相机移动不会再触发听众.
然而,地图工作 – 我(用户)仍然可以移动相机.我(应用程序)不能再用它了.

这是裸码,只有地图创建和处理.其他(实际绘图)被删除

public class MapActivity : Activity,IOnMapReadyCallback,GoogleMap.IOnCameraIdleListener,GoogleMap.IOnCameraMoveStartedListener
{
    private GoogleMap _map;
    private MapFragment _mapFragment;

    private void InitializeMap()
    {
        _mapFragment = MapFragment.NewInstance();
        var tx = FragmentManager.BeginTransaction();
        tx.Add(Resource.Id.map_placeholder,_mapFragment);
        tx.Commit();
    }

    private void SetMapListeners()
    {
        Log.Debug("MyApp/ Map","SetMapListeners");
        _map.SetOnCameraIdleListener(this);
        _map.SetOnCameraMoveStartedListener(this);
    }

    /* Activity */

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Log.Debug("MyApp / Map","OnCreate");
        SetContentView(Resource.Layout.Map);
        InitializeMap();
    }

    protected override void OnStart()
    {
        base.OnStart();
        Log.Debug("MyApp / Map","OnStart");
    }

    protected override void OnResume()
    {
        base.OnResume();
        if (_map == null)
            _mapFragment.GetMapAsync(this);
        Log.Debug("MyApp / Map","OnResume");
    }

    protected override void OnPause()
    {
        base.OnPause();
        Log.Debug("MyApp / Map","OnPause");
    }

    protected override void OnStop()
    {
        base.OnStop();
        Log.Debug("MyApp / Map","OnStop");
    }

    protected override void OnDestroy()
    {
        base.OnStop();
        Log.Debug("MyApp/ Map","OnDestroy");
    }


    /* IOnMapReadyCallback */   

    public void OnMapReady(GoogleMap googleMap)
    {
        Log.Debug("MyApp / Map","Map is ready!");
        _map = googleMap;       
        SetMapListeners();
    }


    /* IOnCameraIdleListener */

    public void OnCameraIdle()
    {
        Log.Debug("MyApp / Map","Camera is idle.");
        // Drawing routine is called here
    }


    /* IOnCameraMoveStartedListener */

    public void OnCameraMoveStarted(int reason)
    {
        Log.Debug("MyApp / Map","Camera move started.");
    }
}

正如您在以下日志摘录中可以看到的,侦听器在开始时工作,但是一旦设备旋转(至少)一次,它们就会消失.
我也尝试在生命周期中调用SetMapListeners一次,第一次调用OnMapReady,但是没有改变任何东西.

04-03 20:29:06.486 D/MyApp / Map( 7446): OnCreate
04-03 20:29:06.688 I/Google Maps Android API( 7446): Google Play services client version: 10084000
04-03 20:29:06.695 I/Google Maps Android API( 7446): Google Play services package version: 10298438
04-03 20:29:07.394 D/MyApp / Map( 7446): OnStart
04-03 20:29:07.399 D/MyApp / Map( 7446): OnResume
04-03 20:29:07.432 D/MyApp / Map( 7446): Map is ready!
04-03 20:29:07.438 D/MyApp / Map( 7446): SetMapListeners
04-03 20:29:07.568 D/MyApp / Map( 7446): Camera is idle.
04-03 20:29:09.231 D/MyApp / Map( 7446): Camera move started.
04-03 20:29:09.590 D/MyApp / Map( 7446): Camera is idle.
04-03 20:29:12.350 D/MyApp / Map( 7446): Camera move started.
04-03 20:29:12.751 D/MyApp / Map( 7446): Camera is idle.

## Listeners are responding,now rotating the device.

04-03 20:29:15.503 D/MyApp / Map( 7446): OnPause
04-03 20:29:15.508 D/MyApp / Map( 7446): OnStop
04-03 20:29:15.572 D/MyApp / Map( 7446): OnDestroy
04-03 20:29:15.595 I/Google Maps Android API( 7446): Google Play services package version: 10298438
04-03 20:29:15.596 D/MyApp / Map( 7446): OnCreate
04-03 20:29:15.628 I/Google Maps Android API( 7446): Google Play services package version: 10298438
04-03 20:29:15.655 D/MyApp / Map( 7446): OnStart
04-03 20:29:15.655 D/MyApp / Map( 7446): OnResume
04-03 20:29:15.690 D/MyApp / Map( 7446): Map is ready!
04-03 20:29:15.691 D/MyApp / Map( 7446): SetMapListeners

## Map is rotated,camera position was preserved. 
## Now moving the camera,but no listeners are responding.

04-03 20:29:24.436 D/MyApp / Map( 7446): OnPause
04-03 20:29:31.288 D/MyApp / Map( 7446): OnStop
04-03 20:29:31.359 D/MyApp / Map( 7446): OnDestroy

对我来说有趣的是,当我切换回上一个活动并再次打开地图时,它会开始新鲜,再次工作.但是,正如您在日志中看到的那样,在旋转设备的同时,活动也被破坏并新创建.据我所知,片段不是,所以也许这是暗示.我不知道.

我也尝试删除OnDestroy中的监听器(通过设置null),但也没有改变任何东西.

你有什么想法我可能做错了吗?

解决方法

尝试检查savedInstanceState以查看您的Activity是否是第一次构建,或者是否只是返回它.您似乎在每次呼叫时都是重叠的片段.
原文链接:https://www.f2er.com/csharp/97124.html

猜你在找的C#相关文章