C# 传递变量的值
我正在为UWP编写应用程序 我有PCL,在那里我编写了从OpenWeather下载json的方法 这是代码C# 传递变量的值,c#,visual-studio,uwp,C#,Visual Studio,Uwp,我正在为UWP编写应用程序 我有PCL,在那里我编写了从OpenWeather下载json的方法 这是代码 public class WeatherRepositoryForUWP { private string url = "http://api.openweathermap.org/data/2.5/weather?q=London&units=imperial&APPID=274c6def18f89eb1d9a444822d2574b5";
public class WeatherRepositoryForUWP
{
private string url = "http://api.openweathermap.org/data/2.5/weather?q=London&units=imperial&APPID=274c6def18f89eb1d9a444822d2574b5";
public async void DownloadWeather()
{
var json = await FetchAsync(url);
Debug.WriteLine(json.ToString());
}
public async Task<string> FetchAsync(string url)
{
string jsonString;
using (var httpClient = new System.Net.Http.HttpClient())
{
//var stream = httpClient.GetStreamAsync(url).Result;
var stream = await httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(stream);
jsonString = reader.ReadToEnd();
}
return jsonString;
}
// Classes for parser
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public double temp_min { get; set; }
public int temp_max { get; set; }
}
public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public double message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class RootObject
{
public Coord coord { get; set; }
public List<Weather> weather { get; set; }
public string @base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
}
}
我需要从city_name中获取值,并将其传递给PCL中的类,然后写入字符串变量
我怎么能做到
谢谢你的帮助 为了能够检索自定义城市的天气信息,您需要通过以下方式更改
WeatherRepositoryForUWP
类:
public class WeatherRepositoryForUWP
{
private string urlFormat = "http://api.openweathermap.org/data/2.5/weather?q={0}&units=imperial&APPID=274c6def18f89eb1d9a444822d2574b5";
public async Task<string> DownloadWeather(string city)
{
return await FetchAsync(string.Format(urlFormat, city));
}
// Rest of class remains unchanged....
}
为了能够检索自定义城市的天气信息,您需要通过以下方式更改
WeatherRepositoryForUWP
类:
public class WeatherRepositoryForUWP
{
private string urlFormat = "http://api.openweathermap.org/data/2.5/weather?q={0}&units=imperial&APPID=274c6def18f89eb1d9a444822d2574b5";
public async Task<string> DownloadWeather(string city)
{
return await FetchAsync(string.Format(urlFormat, city));
}
// Rest of class remains unchanged....
}
所以你想检索一个定制城市的天气信息,而不仅仅是伦敦?什么是PCL,你想实现什么?是的,你是对的@botond.botosPCL-Portable类库@MyBirthNames您想检索自定义城市的天气信息,而不仅仅是伦敦吗?什么是PCL,您想实现什么?是的,您是对的@botond.botosPCL-Portable类库@mybirthnameThank's,我会试试
private void city_button_Click(object sender, RoutedEventArgs e)
{
string city_name;
city_name = city_text.Text;
var weatherService = new WeatherRepositoryForUWP();
string weatherData = weatherService.DownloadWeather(city_name).Result;
}