Luis Valencia
Luis Valencia

Reputation: 33988

How to call an asynchronous method?

I am trying to do a simple app that gets the current location in windows 8, but I cant find the correct syntax of the await keyword.

The error says: Error 1 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'.

The code is as follows:

public MainPage()
        {
            this.InitializeComponent();

            TextBlock txt = new TextBlock();           
            var location = await InitializeLocationServices();
            txt.Text = location;

            Grid.SetRow(txt, 0);
            Grid.SetColumn(txt, 1);
            //InitializeLocationServices();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private async Task<string> InitializeLocationServices()
        {
            //Initialize geolocator object
            Geolocator geoLocator = new Geolocator();
            if (null != geoLocator)
                try
                {
                    //Try resolve the current location
                    var position = await geoLocator.GetGeopositionAsync();
                    if (null != position)
                    {
                        string city = position.CivicAddress.City;
                        string country = position.CivicAddress.Country;
                        string state = position.CivicAddress.State;
                        string zip = position.CivicAddress.PostalCode;
                        string msg = "I am located in " + country;
                        if (city.Length > 0)
                            msg += ", city of " + city;
                        if (state.Length > 0)
                            msg += ", " + state;
                        if (zip.Length > 0)
                            msg += " near zip code " + zip;
                        return msg;
                    }
                    return string.Empty;
                }
                catch (Exception)
                {
                    //Nothing to do - no GPS signal or some timeout occured.n .
                    return string.Empty;
                }
            return string.Empty;
        }

Upvotes: 0

Views: 6716

Answers (2)

jflood.net
jflood.net

Reputation: 2456

so it's not going to work because you are calling it in the constructor.

I'm not familiar with Win8 but from the description of OnNavigatedTo "/// property is typically used to configure the page."

It might be a good candidate for initialisation. Lets try moving it there:

protected async override void OnNavigatedTo(NavigationEventArgs e)
{

   TextBlock txt = new TextBlock();           
   var location = await InitializeLocationServices();
   txt.Text = location;

   Grid.SetRow(txt, 0);
   Grid.SetColumn(txt, 1);
}

Upvotes: 3

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

Remember that your function returns Task<string>, so how comes you returning string twice?

return string.Empty;

on side note .I can't understand this check

Geolocator geoLocator = new Geolocator();
        if (null != geoLocator)

Upvotes: 1

Related Questions