How To solve the following error in windows phone 8 application?

1

I'm developing windows phone 8 application.

I need to get user current location Details.

I try with following code taken from MSDN

C# :

 1  private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)
 2   {if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
 3      {
 4               return;
 5       }  
 6       Geolocator geolocator = new Geolocator();
 7       geolocator.DesiredAccuracyInMeters = 50;
 8       try
 9       {
 10           Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromMinutes(5),
               timeout: TimeSpan.FromSeconds(10)
                );

            LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00");
            LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.00");
        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                // the application does not have the right capability or the location master switch is off
                StatusTextBlock.Text = "location  is disabled in phone settings.";
            }
            //else
            {
                // something else happened acquring the location
            }
        }
    }

I Got error in line number 10.

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

I 'm new to windows phone application. now only i start the learn basic in WP8 .

Plz tell how to solve this ...

c#
xaml
windows-phone-8
geolocation
location
asked on Stack Overflow Mar 19, 2014 by Gurunathan

2 Answers

2

Follow the error instruction. Change

private void OneShotLocationButton_Click

to

private async void OneShotLocationButton_Click
answered on Stack Overflow Mar 19, 2014 by lisp
2

In your code you have used await geolocator.GetGeopositionAsync() now you can only use await with async methods.

so what you have to do is whenever you used async method just declare the method aysnc

like:private async void OneShotLocationButton_Click(object sender, RoutedEventArgs e)

instead of private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)

answered on Stack Overflow Mar 20, 2014 by Arpan.exe

User contributions licensed under CC BY-SA 3.0