Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

c# - How to display an icon from a json string in a Label

I have: api and want to display Icon in array weather -> Icon.

For now I display only the string in this API on the display screen on iOS but I can't see the icon image.

My MainPage.xaml look like this:

             <Frame CornerRadius="10"  
                   OutlineColor="Red"
                   Margin="10,0,10,0"
                   BackgroundColor="White"
                   Opacity="0.5">

                <StackLayout VerticalOptions="Center">          
                    <Label x:Name="infoLabel"
                                   Grid.ColumnSpan="3"
                                   TextColor="Black"
                                   HorizontalOptions="Center"
                                   FontAttributes="Bold" 
                                   FontSize="15" />
                     <Label x:Name="infoLabel1"
                                   Grid.ColumnSpan="3"
                                   TextColor="Black"
                                   HorizontalOptions="Center"
                                   FontAttributes="Bold" 
                                   FontSize="15" />
                </StackLayout> 
            </Frame>

My MainPage.xaml.cs look like this:

string Info = DateDays.DayOfWeek + DateDays.ToString(" MM/dd - HH:mm") + " " + weatherData.weatherForecast[0].Weather.Icon + " " + weatherData.weatherForecast[0].Temperature + "°C";
            infoLabel.Text = Info;

            string Info1 = DateDays1.DayOfWeek + DateDays1.ToString(" MM/dd - HH:mm") + " " + weatherData.weatherForecast[1].Weather.Icon + " " + weatherData.weatherForecast[1].Temperature + "°C";
            infoLabel1.Text = Info1;

And the result is:

What is the correct way to show the icon?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have to translate the code "r01n" into a url. See https://www.weatherbit.io/api/codes

<Image Source="https://www.weatherbit.io/static/img/icons/r01n.png" />

you can build the url like this

var url = $"https://www.weatherbit.io/static/img/icons/{weatherData.weatherForecast[0].Weather.Icon}.png";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...