-1

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

    }
user7285912
  • 201
  • 3
  • 12

1 Answers1

0

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

    }
jgoldberger - MSFT
  • 5,978
  • 2
  • 20
  • 44