Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...