2014-03-07 41 views
0

我在桌面應用程序中編寫代碼,將在警車中使用新的Windows Geolocation API。我爲Geolocator.PositionChanged事件編寫了一個事件監聽器,但它沒有被調用。在我閱讀文檔時,Gelocator似乎只在位置發生變化時引發此事件。異步調用異步方法

我覺得我的程序在完成設置事件監聽器後應該做的第一件事是調用Geolocator.GetPositionAsync方法獲取當前位置,然後讓位置在它們發生時進行更新。我需要同步調用Geolocator.GetPositionAsync方法。爲此,我寫了下面的方法:

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

不過,我得到以下編譯器錯誤就行與await它:

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

這裏有using語句的頂部文件:

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; 

如何解決此編譯器錯誤?

編輯

後,我添加了一個參考System.Runtime.WindowsRuntime.dll項目,錯誤就走開了。但是,現在我得到一個新的錯誤:

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

我該如何解決這個問題?

編輯

下面是調用該方法的代碼:

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

你確定你已經在使用系統;'在同一個文件,你有await表達式?另外請注意,我懷疑你想'返回等待Geolocator.GetGeopositionAsync();' –

+0

是的,我在saem文件中有'using System;'作爲await表達式。我添加了'return'到行,我得到了同樣的錯誤。 –

回答

4

謝謝Scott的幫助。我能夠找出哪些DLL添加引用從您的建議&然後嘗試的事情。

要解決這個問題,我需要將引用添加到:

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

一旦我這樣做,錯誤就走開了。

我希望MS會添加關於哪些DLL需要被引用以使用VS2012中的Windows 8 API到他們的文檔的信息。

+0

不錯非常有幫助,謝謝。 :) –

1

可能重複的問題,但如果你的避風港」,你應該添加到System.Runtime.WindowsRuntime.dll的引用以及已經。

+1

謝謝,畢竟我沒有那個參考。這擺脫了那個錯誤,但現在我得到另一個:'System.Runtime.CompilerServices.TaskAwaiter'1 '不包含'IsCompleted'的定義 –

+0

你想訂閱到一個IsCompleted事件?我覺得你有一些我們沒有看到的代碼。 – Ageonix

+0

我會將該方法的調用添加到OP中。 –

1

我想你在嘗試解決錯誤的問題時遇到了這個問題。

雖然調查寫我的Exposing the Geolocator as a Reactive ServiceNokia Developer Wiki我遇到了同樣的問題(沒有改變事件),並嘗試相同的解決方案(呼籲Geolocator.GetGeopositionAsync)。

事實證明,您需要設置一些屬性(Geolocator.MovementThreshold,Geolocator.DesiredAccuracy,Geolocator.ReportInterval)與一些值。

如果您不熟悉Reactive Extensions (Rx),您應該嘗試一下。您可以從my CodeProject Workspace獲得我的實施。

+0

謝謝。我將這些屬性設置爲我想要使用的值,但我沒有獲得任何位置更新。再次,它使用Wi-Fi來確定我的機器的座標,所以位置不太可能改變。你有任何關於價值的建議嗎? –