Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c# - Xamarin - Geolocation doesnt work... But why

I am just trying to develope a little app, that gives me my current position. So I found this plugin that I entered to my code. Now, that I got everything to compile - the app starts but then, without warning ends. No crash, no nothing. Even Xamarin doesnt show any sign of crash. Can you guys help me out? I have tried nothing, and Im all out of ideas... ;) thanks again!

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;  
        }

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you are not awaiting all calls to async methods, exceptions might be getting swallowed, hiding the cause of the crash.

Perhaps try the following and see if you can get an exception that tells you something:

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;  
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...