0

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;
        }
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • the issue is you're trying to call it in a constructor, this is not possible i dont think, not to mention a horrible idea to begin with. This has SO question has an answer for "CAn constructors be async" http://stackoverflow.com/questions/8145479/can-constructors-be-async – jflood.net Jun 10 '12 at 11:20
  • can you please provide code how can I fix this? I dont care to move it from the constructor. – Luis Valencia Jun 10 '12 at 11:24
  • dunno, not familiar with win8 apps page lifecycle, maybe try calling it OnNavigatedTo and mark that as async – jflood.net Jun 10 '12 at 11:30

2 Answers2

3

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);
}
jflood.net
  • 2,446
  • 2
  • 21
  • 19
  • I don't think `InitializeComponent()` belongs there, but otherwise, I think something like this is the best solution (although another method might be more suitable, I don't know WinRT that well either). – svick Jun 10 '12 at 12:08
  • agreed, removed InitializeComponent() – jflood.net Jun 10 '12 at 12:42
1

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)
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • from here: http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2012/02/29/windows-8-consumer-preview-and-visual-studio-11-beta-quick-tip-using-location-services-part-8-11.aspx – Luis Valencia Jun 10 '12 at 11:23
  • how can I change my code to make the method called asynchronously based on that page?, yes that if check should be backwards lol – Luis Valencia Jun 10 '12 at 11:24
  • more like the null check is pointless, geolocator is being instantiated right above it, it will never be null at the null check – jflood.net Jun 10 '12 at 11:25
  • Having two or more `return` statements in one method is perfectly fine, there's nothing wrong about it. – svick Jun 10 '12 at 11:28
  • yeah idea how to change this code to make it compile and work? I just want a textbox to have the current location when the application starts, but I want to do it async – Luis Valencia Jun 10 '12 at 11:30