2016-11-22 30 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图书馆。

相关问题