Xamarin - 地理定位不起作用......但为什么呢

时间:2017-03-22 20:16:40

标签: c# xamarin

我只是想开发一个小应用程序,这给了我现在的位置。所以我找到了我输入到我的代码中的插件。现在,我得到了所有编译的东西 - 应用程序启动但随后,没有警告结束。没有崩溃,没有什么。即使是Xamarin也没有出现任何崩溃的迹象。你能帮助我吗?我没有尝试任何东西,而且我的想法都是......;) 再次感谢!

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            //Initialize buttons:

            Button StartButton = FindViewById<Button>(Resource.Id.startbutton);
            TextView txtTestGps = FindViewById<TextView>(Resource.Id.GpsTest);

            ShowGpsCoordinates(StartButton, txtTestGps);
        }

        private void ShowGpsCoordinates(Button StartButton, TextView txtTestGps) 
        {
           Task<double> xy = GiveGpsLocation();
           double xyOut = xy.Result; 

            StartButton.Click += (sender, e) =>
            {
                txtTestGps.Text = xyOut.ToString();   
            };
        }

        private async Task<double> GiveGpsLocation()
        {
            double DoubleWithCoordinates = 0.0;

            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;

            var position =  await locator.GetPositionAsync(10000);

            // Console.WriteLine("Position Status: {0}", position.Timestamp);
            // Console.WriteLine("Position Latitude: {0}", position.Latitude);
            // Console.WriteLine("Position Longitude: {0}", position.Longitude);

            DoubleWithCoordinates = position.Latitude; 

            return DoubleWithCoordinates;  
        }

    }

1 个答案:

答案 0 :(得分:0)

由于您没有等待对异步方法的所有调用,因此可能会吞下异常,从而隐藏崩溃的原因。

也许尝试以下操作,看看是否可以获得一个告诉你的异常:

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            //Initialize buttons:

            Button StartButton = FindViewById<Button>(Resource.Id.startbutton);
            TextView txtTestGps = FindViewById<TextView>(Resource.Id.GpsTest);

            Task.Run(async () => {
                await ShowGpsCoordinates(StartButton, txtTestGps);
            }
        }

        private async Task ShowGpsCoordinates(Button StartButton, TextView txtTestGps) 
        {
           double xyOut = await GiveGpsLocation();


            StartButton.Click += (sender, e) =>
            {
                txtTestGps.Text = xyOut.ToString();   
            };
        }

        private async Task<double> GiveGpsLocation()
        {
            double DoubleWithCoordinates = 0.0;

            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;

            var position =  await locator.GetPositionAsync(10000);

            // Console.WriteLine("Position Status: {0}", position.Timestamp);
            // Console.WriteLine("Position Latitude: {0}", position.Latitude);
            // Console.WriteLine("Position Longitude: {0}", position.Longitude);

            DoubleWithCoordinates = position.Latitude; 

            return DoubleWithCoordinates;  
        }

    }