2016-11-22 32 views
0

我正在編寫UWP應用程序。爲PCL製作UWP應用程序的位置定義

我爲UWP項目創建了PCL。

它下載緯度和經度的數據(這是天氣應用程序)。此外,我需要爲智能手機的位置定義緯度和經度。

這裏是我的代碼:

public class OpenWeatherViewModel 
{ 
    private const string APPID = "f3c45b5a19426de9ea6ba7eb6c6969d7"; 
    private List<RootObject> weatherList; 

    public List<RootObject> WeatherListList 
    { 
     get { return weatherList; } 
     set { weatherList = value; } 
    } 

    public OpenWeatherViewModel() 
    { 
     Data_download(); 
    } 




    public async void Data_download() 
    { 
     var geoLocator = new Geolocator(); 
     geoLocator.DesiredAccuracy = PositionAccuracy.High; 
     Geoposition pos = await geoLocator.GetGeopositionAsync(); 
     string latitude = "Latitude: " + pos.Coordinate.Point.Position.Latitude.ToString(); 
     string longitude = "Longitude: " + pos.Coordinate.Point.Position.Longitude.ToString(); 
     var url = String.Format(
      "http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units=metric&APPID=" + APPID, latitude, longitude); 
     var json = await FetchAsync(url); 



     List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(json); 

     WeatherListList = new List<RootObject>(rootObjectData); 
    } 

    public async Task<string> FetchAsync(string url) 
    { 
     string jsonString; 

     using (var httpClient = new System.Net.Http.HttpClient()) 
     { 
      var stream = await httpClient.GetStreamAsync(url); 
      StreamReader reader = new StreamReader(stream); 
      jsonString = reader.ReadToEnd(); 
     } 

     return jsonString; 
    } 

該行Geoposition pos = await geoLocator.GetGeopositionAsync();我有錯誤:Error CS0012 The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows.Foundation.FoundationContract, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.

我怎樣才能解決這個問題?

感謝您的幫助。

回答

1

可移植類庫的目的是幫助構建跨平臺的應用程序和庫,在應用程序的不同部分之間共享代碼。

在VS 2015中創建PCL時,可以指定Windows 10通用應用程序的API類型。但是在這裏,這種方法只適用於WinRT應用程序,而不是傳統的Win32應用程序,我認爲將它放入PCL並不是一個好設計,您可以將這些代碼移動到UWP應用程序中。

或者,如果你只是想爲你的應用程序UWP創建庫,你可以創建一個Class Library (Universal Windows)而不是創建Class Library (Portable)

enter image description here

可以比較這兩種不同的PCLS的References

類庫(便攜式):

enter image description here

類庫(通用於Windows):上述

enter image description here

圖像中的引用使你們班使用UWP的API圖書館。

相關問題