【问题标题】:How to display an icon from a json string in a Label如何在标签中显示来自 json 字符串的图标
【发布时间】:2021-01-12 18:34:31
【问题描述】:

我有:api 并想在数组天气中显示图标 -> 图标。

目前我只在 iOS 的显示屏上显示此 API 中的字符串,但看不到图标图像。

我的MainPage.xaml 看起来像这样:

             <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>

我的MainPage.xaml.cs 看起来像这样:

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;

结果是:

显示图标的正确方法是什么?

【问题讨论】:

  • 最好先阅读描述天气图标工作原理的 API 文档
  • 什么意思?
  • 文档向您展示如何将“r01n”翻译成图片网址
  • 你能给我一些带有文档的网址吗?

标签: c# ios xamarin xamarin.forms


【解决方案1】:

您必须将代码“r01n”翻译成网址。见https://www.weatherbit.io/api/codes

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

你可以像这样构建网址

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

【讨论】:

  • 你能给我一些例子,如何用 if else statemant 替换 .png 之前的名称吗?
  • 查看扩展示例
  • 完美工作!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 2013-01-29
  • 2017-07-30
  • 2023-04-07
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
相关资源
最近更新 更多