简体   繁体   English

如何显示class中声明的变量

[英]How to display the variable declared in a class

I created a class called Weather as below:我创建了一个名为Weather的 class,如下所示:

public class Weather
{
    public string WeatherName { get; set; }
    public int WeatherId { get; set; }
}

And then, I would like to get the data from an URL and display the data I've got.然后,我想从 URL 获取数据并显示我得到的数据。

public partial class HomePage : ContentPage
{
    
    public HomePage()
    {
        InitializeComponent();
        GetWeather();
    }

    async public void GetWeather()
    {
        string url = "the URL....";

        var handler = new HttpClientHandler();
        HttpClient client = new HttpClient(handler);
        string result = await client.GetStringAsync(url);

        var resultObject = JObject.Parse(result);
        int WeatherId = (int)resultObject["icon"][0]; //get the "icon" data from the JSON file
        Weather weather = new Weather();
        weather.WeatherId = WeatherId;
        switch (WeatherId)
        {
            //setting the WeatherName according to the WeatherId...
        }
    }
    
}

I would like to display the WeatherId and the WeatherName , but I have no idea how to do that.我想显示WeatherIdWeatherName ,但我不知道该怎么做。 Please give me a simple solution as I may not understand some difficult codes.请给我一个简单的解决方案,因为我可能不理解一些困难的代码。

in your page, add some UI elements to display the data在您的页面中,添加一些 UI 元素来显示数据

// these go inside of the page's Content tags
<StackLayout>
  <Label x:Name="WeatherIDLabel" />
  <Label x:Name="WeatherNameLabel" />
</StackLayout>

then in your code when you retrieve the data然后在您检索数据时在您的代码中

WeatherIDLabel.Text = weather.WeatherId;
WeatherNameLabel.Text = weather.WeatherName;

there are thousands of excellent tutorials, videos, sample apps, etc that illustrate how to build an app from scratch.有数以千计的优秀教程、视频、示例应用程序等可以说明如何从头开始构建应用程序。 I suggest you take the time to walk through some of them我建议你花时间浏览其中的一些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM