0

I'm writing code in a desktop application that will be used in police cars to use the new Windows Geolocation API. I've written an event listener for the Geolocator.PositionChanged event, but it's not getting called. In my reading of the documentation, it seems that the Gelocator only raises this event when the position changes.

I figure the first thing my program should do after it finished setting up the event listener is to call the Geolocator.GetPositionAsync method to get the current position and then let the position get updated as they happen.

I need to call the Geolocator.GetPositionAsync method synchronously. To that end, I wrote the following method:

private async Task<Geoposition> GetCurrentPosition() {
    await Geolocator.GetGeopositionAsync();
}

However, I get the following compiler error on the line with await in it:

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

Here are the using statements at the top of the file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Windows.Devices.Geolocation;

How do I fix this compiler error?

Edit

After I added a reference to System.Runtime.WindowsRuntime.dll the project, the error went away. However, now I'm getting a new error:

'System.Runtime.CompilerServices.TaskAwaiter`1<Windows.Devices.Geolocation.Geoposition>' does not contain a definition for 'IsCompleted'

How do I fix this one?

Edit

Here's the code that calls the method:

protected override void Initialize() {
    // Make sure we "forget" any previous Gelocator device
    if ( Geolocator != null ) {
        Geolocator.PositionChanged -= PositionChanged;
        Geolocator.StatusChanged   -= StatusChanged;
        Geolocator                  = null;
    }

    CurrentPosition = new GpsInformation() { TimeStamp = DateTime.Now };
    Geolocator = new Geolocator();
    Geolocator.DesiredAccuracy   = PositionAccuracy.High;    // Sends position updates with the highest accuracy possible.
    Geolocator.MovementThreshold = 0;                        // Sends position updates no matter how far the vehicle has moved since the last update.
    Geolocator.ReportInterval    = 0;                        // Sends position updates as they become availble.
    Geolocator.PositionChanged  += PositionChanged;          // Sends position updates to this method.
    Geolocator.StatusChanged    += StatusChanged;            // Sends status changed updates to this method.
    switch ( Geolocator.LocationStatus ) {
        case PositionStatus.Disabled:
        case PositionStatus.NotAvailable: 
            // The Geolocator device is disabled.  We won't get any updates.  Throw an exception.
            throw new Exception( "LPRCore does not have permission to use Windows Geolocation services or Geolocation services are not working." );
    }

    Task<Geoposition> getPositionTask = GetCurrentPosition();
    positionRecord = getPositionTask.Result;
}
Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • 2
    You're sure you've got `using System;` in the same file that you've got the await expression? Also note that I suspect you want `return await Geolocator.GetGeopositionAsync();` – Jon Skeet Mar 07 '14 at 16:32
  • Yes, I do have the `using System;` in the saem file as the await expression. I added the `return ` to the line & I'm getting the same error. – Tony Vitabile Mar 07 '14 at 16:38

3 Answers3

4

Thank you Scott for your help. I was able to figure out which DLLs to add references to from your suggestions & then hit-or-miss trying things.

To fix the problem, I needed to add references to:

  • System.Threading.dll and
  • System.Threading.Tasks.dll

Once I did this, the error went away.

I wish MS would add information about which DLLs need to be referenced to use the Windows 8 APIs from VS2012 to their documentation.

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
1

Possible duplicate question, but you should add a reference to System.Runtime.WindowsRuntime.dll as well if you haven't already.

Ageonix
  • 1,748
  • 2
  • 19
  • 32
  • 1
    Thanks, I didn't have that reference after all. That got rid of that error, but now I'm getting another: 'System.Runtime.CompilerServices.TaskAwaiter`1' does not contain a definition for 'IsCompleted' – Tony Vitabile Mar 07 '14 at 16:44
  • Are you trying to subscribe to an IsCompleted event? I feel like you have some code that we're not seeing here. – Ageonix Mar 07 '14 at 16:54
  • I'll add the call to that method to the OP. – Tony Vitabile Mar 07 '14 at 17:06
  • All of the code that has to do with this call is now in the original question. – Tony Vitabile Mar 07 '14 at 17:08
  • Shot in the dark here, but try referencing System.RunTime; as well. – Ageonix Mar 07 '14 at 17:26
  • Ok, two other things to try and then I'm all out of options. Try adding a reference to System.Runtime.InteropServices.WindowsRuntime.dll and try setting the target platform to x64 if you're on a 64-bit machine. – Ageonix Mar 07 '14 at 17:39
  • I should have added the references to the post. Yes, I already have a reference to `System.Runtime.InteropServices.WindowsRuntime.dll`. And yes, I set the target platform to x64. Didn't make any difference. – Tony Vitabile Mar 07 '14 at 17:50
1

I think you ran into this problem while trying to resolve the wrong issue.

While investigation to write my Exposing the Geolocator as a Reactive Service on the Nokia Developer Wiki I came across the same problem (no change events) and tried the same solution (calling Geolocator.GetGeopositionAsync).

It turns out that you need to set some properties (Geolocator.MovementThreshold, Geolocator.DesiredAccuracy, Geolocator.ReportInterval) with some values.

If you are not familiar with the Reactive Extensions (Rx), you should try it. You can get my implementation from my CodeProject Workspace.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • Thanks. I'm setting those properties to values I want to use and I wasn't getting any position updates. Then again, it's using Wi-Fi to determine my machine's coordinates, so the position isn't likely to change. Do you have any recommendations for values? – Tony Vitabile Mar 10 '14 at 16:16