WPF.NET6项目—调用OpenweatherMap API获取指定城市的天气,温度等数据

让ChatGTP帮我写的程序

问:用WPF C#怎么获取指定城市的天气?
ChatGTP答:
要获取指定城市的天气,您可以使用 WPF 和 C# 结合使用,通过调用天气数据的 API 来获取天气信息。以下是一个简单的示例,演示如何使用 WPF 和 C# 获取指定城市的天气。
首先,您需要选择一个提供天气数据的 API,并获取相应的 API 密钥。一些常用的天气 API 包括 OpenWeatherMap、Weather.com、和 AccuWeather 等。在这个示例中,我们使用 OpenWeatherMap API。
接下来,您需要在项目中添加一个用于显示天气信息的界面。在 WPF 中,您可以使用 XAML 创建界面元素。下面是一个示例的 XAML 代码,显示一个用于输入城市名称和显示天气信息的界面:
xaml

<Window x:Class="WeatherApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Weather App" Height="350" Width="525">
    <Grid>
        <StackPanel Margin="10">
            <TextBlock Text="Enter City:"/>
            <TextBox x:Name="txtCity" Width="200"/>
            <Button Content="Get Weather" Click="btnGetWeather_Click" Margin="0,10,0,0"/>
            <TextBlock Text="Weather Information:" Margin="0,20,0,0"/>
            <TextBlock x:Name="txtWeatherInfo"/>
        </StackPanel>
    </Grid>
</Window>

然后,您需要在代码文件中处理按钮点击事件,并调用天气 API 来获取天气数据。在代码文件中,您可以使用 HttpClient 类来发送 HTTP 请求,并解析返回的 JSON 数据。以下是一个示例的代码:
csharp

using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
namespace WeatherApp
{
    public partial class MainWindow : Window
    {
        private const string ApiKey = "YOUR_API_KEY";
        private const string ApiBaseUrl = "https://api.openweathermap.org/data/2.5/weather";
        public MainWindow()
        {
            InitializeComponent();
        }
        private async void btnGetWeather_Click(object sender, RoutedEventArgs e)
        {
            string city = txtCity.Text;
            string apiUrl = $"{ApiBaseUrl}?q={city}&appid={ApiKey}";
            try
            {
                HttpClient httpClient = new HttpClient();
                HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                // 解析 JSON 数据,提取天气信息
                // 这里需要根据您选择的 API 和其返回的数据结构进行解析
                // 示例:从 OpenWeatherMap API 中提取温度信息
                dynamic weatherData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseBody);
                double temperature = weatherData.main.temp - 273.15; // 转换为摄氏度
                txtWeatherInfo.Text = $"Temperature in {city}: {temperature}°C";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
    }
}

请注意,您需要将上述代码中的 YOUR_API_KEY 替换为您从天气 API 获得的实例。*

————————————————————————————————————————————
接下来我开始了一波复制操作。新建了一个WPF C# .NET6.0的空项目,然后将xaml和后台代码复制过来。但是运行有一个错误,需要添加一个Nuget包,搜索:OpenweatherMap API 并安装。
在这里插入图片描述
接下来需要从官网上获取API密钥。打开官网https://home.openweathermap.org/api_keys
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述运行OK!
在这里插入图片描述
如果需要工程文件,移步:https://download.csdn.net/download/qq_30083165/87934002
觉得有用给点个赞吧~谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤豆糊_siki

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值