-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
312 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,3 +396,6 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
|
||
# API | ||
Secrets.cs |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
android/XPrism/XPrism/Resources/Images/material_weather_partly_cloudy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
android/XPrism/XPrism/Resources/Images/material_weather_partly_cloudy_white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="XPrism.WeatherPage" | ||
Title="设置天气"> | ||
<ScrollView> | ||
<VerticalStackLayout Margin="10"> | ||
<Label Text="选择城市:" FontSize="Title" /> | ||
<!--<Picker x:Name="picker" Title="选择一个城市"> | ||
<Picker.ItemsSource> | ||
<x:Array Type="{x:Type x:String}"> | ||
<x:String>北京</x:String> | ||
<x:String>上海</x:String> | ||
<x:String>深圳</x:String> | ||
<x:String>广州</x:String> | ||
<x:String>杭州</x:String> | ||
<x:String>成都</x:String> | ||
<x:String>南京</x:String> | ||
<x:String>苏州</x:String> | ||
<x:String>重庆</x:String> | ||
<x:String>武汉</x:String> | ||
</x:Array> | ||
</Picker.ItemsSource> | ||
</Picker>--> | ||
|
||
<Entry x:Name="cityEntry" Placeholder="请输入城市名称" /> | ||
|
||
<Button Text="查询" Clicked="OnSearchClicked" /> | ||
|
||
<Label Text="查询结果:" FontSize="Title" /> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="城市:" /> | ||
<Label x:Name="cityLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="天气:" /> | ||
<Label x:Name="weatherLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="温度:" /> | ||
<Label x:Name="temperatureLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="风向:" /> | ||
<Label x:Name="windDirectionLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="风速:" /> | ||
<Label x:Name="windSpeedLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="湿度:" /> | ||
<Label x:Name="humidityLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="更新时间:" /> | ||
<Label x:Name="reportTimeLabel" /> | ||
</StackLayout> | ||
<StackLayout Orientation="Horizontal"> | ||
<Label Text="数据来源:" /> | ||
<Label x:Name="sourceLabel" /> | ||
</StackLayout> | ||
|
||
|
||
</VerticalStackLayout> | ||
</ScrollView> | ||
</ContentPage> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
#define OPENWEATHERMAP // OPENWEATHERMAP or ACCUWEATHER | ||
|
||
using Android.App; | ||
using System.Text.Json; | ||
|
||
namespace XPrism; | ||
|
||
#if OPENWEATHERMAP | ||
public partial class WeatherPage : ContentPage | ||
{ | ||
public string url = "http://api.openweathermap.org/data/2.5/weather"; | ||
public string city = ""; | ||
public string apiKey = Secrets.OpenWeatherAPI; // Get your own API key from https://openweathermap.org/api | ||
public string units = "metric"; | ||
public string lang = "zh_cn"; | ||
public string fullUrl = ""; | ||
|
||
public WeatherPage() | ||
{ | ||
InitializeComponent(); | ||
cityLabel.Text = "N/A"; | ||
weatherLabel.Text = "N/A"; | ||
temperatureLabel.Text = "N/A"; | ||
windDirectionLabel.Text = "N/A"; | ||
windSpeedLabel.Text = "N/A"; | ||
humidityLabel.Text = "N/A"; | ||
reportTimeLabel.Text = "N/A"; | ||
sourceLabel.Text = "OpenWeatherMap"; | ||
} | ||
|
||
public async void OnSearchClicked(object sender, System.EventArgs e) | ||
{ | ||
city = cityEntry.Text; | ||
if (city != "") | ||
{ | ||
fullUrl = $"{url}?q={city}&appid={apiKey}&units={units}&lang={lang}"; | ||
HttpClient client = new HttpClient(); | ||
var response = await client.GetAsync(fullUrl); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var content = await response.Content.ReadAsStringAsync(); | ||
var weather = JsonSerializer.Deserialize<Weather>(content); | ||
if (weather.cod != 200) | ||
{ | ||
await DisplayAlert("错误", "城市名称错误", "确定"); | ||
return; | ||
} | ||
cityLabel.Text = city; | ||
weatherLabel.Text = weather.weather[0].description; | ||
temperatureLabel.Text = weather.main.temp.ToString() + "℃"; | ||
windDirectionLabel.Text = weather.wind.deg.ToString() + "°"; | ||
windSpeedLabel.Text = weather.wind.speed.ToString() + "m/s"; | ||
humidityLabel.Text = weather.main.humidity.ToString() + "%"; | ||
reportTimeLabel.Text = weather.dt.ToString(); | ||
} | ||
else | ||
{ | ||
await DisplayAlert("错误", "城市名称错误", "确定"); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
class Weather | ||
{ | ||
public int cod { get; set; } | ||
public WeatherMain main { get; set; } | ||
public WeatherWind wind { get; set; } | ||
public WeatherWeather[] weather { get; set; } | ||
public int dt { get; set; } | ||
} | ||
class WeatherMain | ||
{ | ||
public float temp { get; set; } | ||
public int humidity { get; set; } | ||
} | ||
class WeatherWind | ||
{ | ||
public float speed { get; set; } | ||
public float deg { get; set; } | ||
} | ||
class WeatherWeather | ||
{ | ||
public string description { get; set; } | ||
} | ||
#endif | ||
#if ACCUWEATHER | ||
public partial class WeatherPage : ContentPage | ||
{ | ||
public string url = "http://dataservice.accuweather.com/currentconditions/v1/"; | ||
public string locationKey = ""; | ||
public string apiKey = Secrets.AccuWeatherAPI; | ||
public string language = "zh-CN"; | ||
public string details = "true"; | ||
public string fullUrl = ""; | ||
|
||
public WeatherPage() | ||
{ | ||
InitializeComponent(); | ||
cityLabel.Text = "N/A"; | ||
weatherLabel.Text = "N/A"; | ||
temperatureLabel.Text = "N/A"; | ||
windDirectionLabel.Text = "N/A"; | ||
windSpeedLabel.Text = "N/A"; | ||
humidityLabel.Text = "N/A"; | ||
reportTimeLabel.Text = "N/A"; | ||
} | ||
|
||
public async void OnSearchClicked(object sender, System.EventArgs e) | ||
{ | ||
switch (picker.SelectedIndex) | ||
{ | ||
case 0: | ||
locationKey = "101924"; | ||
break; | ||
case 1: | ||
locationKey = "106577"; | ||
break; | ||
case 2: | ||
locationKey = "58194"; | ||
break; | ||
case 3: | ||
locationKey = "102255"; | ||
break; | ||
case 4: | ||
locationKey = "106832"; | ||
break; | ||
case 5: | ||
locationKey = "106774"; | ||
break; | ||
case 6: | ||
locationKey = "105570"; | ||
break; | ||
case 7: | ||
locationKey = "105571"; | ||
break; | ||
case 8: | ||
locationKey = "102144"; | ||
break; | ||
case 9: | ||
locationKey = "103847"; | ||
break; | ||
default: | ||
locationKey = ""; | ||
fullUrl = ""; | ||
break; | ||
} | ||
if (locationKey != "") | ||
{ | ||
fullUrl = $"{url}{locationKey}?apikey={apiKey}&language={language}&details={details}"; | ||
HttpClient client = new HttpClient(); | ||
var response = await client.GetAsync(fullUrl); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var content = await response.Content.ReadAsStringAsync(); | ||
if (content == "[]") | ||
{ | ||
cityLabel.Text = "N/A"; | ||
weatherLabel.Text = "N/A"; | ||
temperatureLabel.Text = "N/A"; | ||
windDirectionLabel.Text = "N/A"; | ||
windSpeedLabel.Text = "N/A"; | ||
humidityLabel.Text = "N/A"; | ||
reportTimeLabel.Text = "N/A"; | ||
return; | ||
} | ||
else | ||
{ | ||
var weather = JsonSerializer.Deserialize<Weather>(content); | ||
cityLabel.Text = picker.Items[picker.SelectedIndex]; | ||
weatherLabel.Text = weather.WeatherText; | ||
temperatureLabel.Text = weather.Temperature.Metric.Value.ToString() + "℃"; | ||
windDirectionLabel.Text = weather.Wind.Direction.Localized; | ||
windSpeedLabel.Text = weather.Wind.Speed.Metric.Value.ToString() + "km/h"; | ||
humidityLabel.Text = weather.RelativeHumidity.ToString() + "%"; | ||
reportTimeLabel.Text = weather.LocalObservationDateTime.ToString(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class Weather | ||
{ | ||
public DateTime LocalObservationDateTime { get; set; } | ||
public string WeatherText { get; set; } | ||
public Temperature_ Temperature { get; set; } | ||
public Wind_ Wind { get; set; } | ||
public int RelativeHumidity { get; set; } | ||
public class Metric_ | ||
{ | ||
public double Value { get; set; } | ||
public string Unit { get; set; } | ||
} | ||
public class Temperature_ | ||
{ | ||
public Metric_ Metric { get; set; } | ||
} | ||
public class Direction_ | ||
{ | ||
public string Localized { get; set; } | ||
} | ||
public class Speed_ | ||
{ | ||
public Metric_ Metric { get; set; } | ||
} | ||
public class Wind_ | ||
{ | ||
public Direction_ Direction { get; set; } | ||
public Speed_ Speed { get; set; } | ||
} | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters