【问题标题】:Make Location definition for UWP app in PCL在 PCL 中为 UWP 应用程序定义位置
【发布时间】:2016-11-23 18:32:24
【问题描述】:

我正在编写 UWP 应用程序。

我为 UWP 项目创建了 PCL。

它下载纬度和经度数据(这是天气应用程序)。我还需要为智能手机的位置定义 lat 和 lon。

这是我的代码:

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&lt;&gt;' 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'.

我该如何解决这个问题?

感谢您的帮助。

【问题讨论】:

    标签: c# visual-studio uwp


    【解决方案1】:

    可移植类库的目的是帮助构建跨平台应用和库,在应用的不同部分之间共享代码。

    在 VS 2015 中创建 PCL 时,可以指定 Windows 10 通用应用的 API 类型。但是这里这种方法只适用于 WinRT 应用程序,而不是传统的 Win32 应用程序,我认为把它放在 PCL 中不是一个好的设计,你可以将这些代码移动到你的 UWP 应用程序中。

    或者如果你只是想为你的 UWP 应用创建一个库,你可以创建一个Class Library (Universal Windows) 而不是创建Class Library (Portable)

    您可以比较这两个不同 PCL 的References

    类库(便携式):

    类库(通用 Windows):

    上图中的引用允许在您的类库中使用 UWP api。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2018-05-07
      • 1970-01-01
      • 2019-12-05
      • 2023-03-15
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多