我是C#和Xamarin.Forms的新手,但是我尝试构建一个小型应用,它将显示天气,这将是2天,我无法从天气API获取信息(不知道如何加入)它)。我已经将解决方案依赖项添加到Windows.Csharp和Newtonsoft.Json中,并且什么也行不通,即使从ms.docs应用示例也不行。我在MainPage.xaml.cs中只有几个按钮:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Apppiii1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page1());
}
private async void Button_Clicked_1(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page2());
}
private async void Button_Clicked_2(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page3());
}
private async void Button_Clicked_3(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page4());
}
private async void Button_Clicked_4(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page5());
}
private async void Button_Clicked_5(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page6());
}
}
}
那是我的MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Apppiii1"
x:Class="Apppiii1.MainPage">
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Image Source="dark.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" Aspect="AspectFill"/>
<StackLayout>
<Label TextColor="#77d065" HorizontalOptions="Center" FontSize = "25" Text="Meteo plus grand ville en France." />
<Button Text="Paris" Clicked="Button_Clicked"/>
<Button Text="Lyon" Clicked="Button_Clicked_1"/>
<Button Text="Bordeaux" Clicked="Button_Clicked_2"/>
<Button Text="Lille" Clicked="Button_Clicked_3"/>
<Button Text="Nice" Clicked="Button_Clicked_4"/>
<Button Text="Starsburg" Clicked="Button_Clicked_5"/>
</StackLayout>
</AbsoluteLayout>
</ContentPage>
和我的Page1.xaml.cs
namespace Apppiii1
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public Page1 ()
{
InitializeComponent ();
}
}
}
和Page1.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Apppiii1.Page1">
<ContentPage.Content>
<StackLayout>
<Image Source="android1.png"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
与那是我想加入的代码:在ms网站上分为3部分: 第一
namespace WeatherApp
{
public class Weather
{
// Because labels bind to these values, set them to an empty string to
// ensure that the label appears on all platforms by default.
public string Title { get; set; } = " ";
public string Temperature { get; set; } = " ";
public string Wind { get; set; } = " ";
public string Humidity { get; set; } = " ";
public string Visibility { get; set; } = " ";
public string Sunrise { get; set; } = " ";
public string Sunset { get; set; } = " ";
}
}
第二:
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace WeatherApp
{
public class DataService
{
public static async Task<dynamic> GetDataFromService(string queryString)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(queryString);
dynamic data = null;
if (response != null)
{
string json = response.Content.ReadAsStringAsync().Result;
data = JsonConvert.DeserializeObject(json);
}
return data;
}
}
}
第三名:
using System;
using System.Threading.Tasks;
namespace WeatherApp
{
public class Core
{
public static async Task<Weather> GetWeather(string zipCode)
{
//Sign up for a free API key at http://openweathermap.org/appid
string key = "YOUR API KEY HERE";
string queryString = "http://api.openweathermap.org/data/2.5/weather?zip="
+ zipCode + ",us&appid=" + key + "&units=imperial";
//Make sure developers running this sample replaced the API key
if (key == "YOUR API KEY HERE")
{
throw new ArgumentException("You must obtain an API key from openweathermap.org/appid and save it in the 'key' variable.");
}
dynamic results = await DataService.GetDataFromService(queryString).ConfigureAwait(false);
if (results["weather"] != null)
{
Weather weather = new Weather();
weather.Title = (string)results["name"];
weather.Temperature = (string)results["main"]["temp"] + " F";
weather.Wind = (string)results["wind"]["speed"] + " mph";
weather.Humidity = (string)results["main"]["humidity"] + " %";
weather.Visibility = (string)results["weather"][0]["main"];
DateTime time = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime sunrise = time.AddSeconds((double)results["sys"]["sunrise"]);
DateTime sunset = time.AddSeconds((double)results["sys"]["sunset"]);
weather.Sunrise = sunrise.ToString() + " UTC";
weather.Sunset = sunset.ToString() + " UTC";
return weather;
}
else
{
return null;
}
}
}
}
每次单击按钮时,我想在下一页显示气象信息。请一些身体帮助...