Пакет System.Device.Location.Portable может быть не полностью совместим с вашим проектом.C#

Место общения программистов C#
Ответить
Anonymous
 Пакет System.Device.Location.Portable может быть не полностью совместим с вашим проектом.

Сообщение Anonymous »

Я пытаюсь получить координаты текущего местоположения моего устройства с помощью класса GeoCoordinateWatcher и передать их в запрос API погоды. Но после установки пакета System.Device.Location.Portable у меня возникла следующая ошибка сборки:

Код: Выделить всё

C:\Work\GitHub\WeatherPanel\MainWindow.xaml.cs(38,36,38,56): error CS0234: The type or namespace name 'GeoCoordinateWatcher' does not exist in the namespace 'System.Device.Location' (are you missing an assembly reference?)
предупреждение:

Код: Выделить всё

 warning NU1701: Package 'System.Device.Location.Portable 1.0.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net6.0-windows10.0.19041'. This package may not be fully compatible with your project.
Как устранить эту ошибку сборки или нужно ли установить какой-либо другой пакет Nuget, чтобы код GeoCoordinateWatcher работал? спасибо.
MainWindow.xaml.cs:

Код: Выделить всё

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System;
using System.Device.Location;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace WeatherPanel
{
/// 
/// An empty window that can be used on its own or navigated to within a Frame.
/// 
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
_ = Main();
}

async Task Main()
{
GeoCoordinateWatcher watcher;
watcher = new GeoCoordinateWatcher();

watcher.PositionChanged += (sender, e) =>
{
var coordinate = e.Position.Location;
Console.WriteLine("Lat: {0}, Long: {1}", coordinate.Latitude,
coordinate.Longitude);
// Uncomment to get only one event.
watcher.Stop();
};

string apiKey = "YOUR_API_KEY";
string city = "NewYork";
//string location = "48.8567,2.3508"
string apiUrl = $"http://api.weatherapi.com/v1/current.json?key={apiKey}&q={city}&aqi=no";

try
{
string weatherData = await GetWeatherData(apiUrl);
// Parse and extract relevant information from the JSON response
// For simplicity, let's assume you're interested in the temperature only
string temperature = ParseValues(weatherData, "temp_c");
txtTemp.Text = "Today : " + temperature.ToString() + " °C";
string currentStatus = ParseValues(weatherData, "text");
txtStatus.Text = currentStatus.ToString();
string precipitation = ParseValues(weatherData, "precip_in");
txtPrecip.Text = "Precipitation : " + precipitation.Substring(1,precipitation.Length-1).ToString() + " in";
string wind = ParseValues(weatherData, "wind_mph");
txtWind.Text = "Wind : " + wind.ToString() + " mph";
string humidity = ParseValues(weatherData, "humidity");
txtHumidity.Text = "Humidity : " + humidity.ToString();
string feelsLike = ParseValues(weatherData, "feelslike_c");
txtFeelsLike.Text = "Feels Like : " + feelsLike.ToString() + " °C";
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
Исходный код проекта:
https://github.com/Ron391/WeatherPanel

Подробнее здесь: https://stackoverflow.com/questions/786 ... th-your-pr
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»