【问题标题】:How to display weather icon in xamarin by calling Openweather API?如何通过调用 Openweather API 在 xamarin 中显示天气图标?
【发布时间】:2016-12-25 06:09:36
【问题描述】:

我正在开发 XAMARIN 应用程序。我正在调用开放天气 API 并能够获得 JSON 响应。

字符串格式后,我可以在标题(标题)中显示城市名称+温度。

还是无法绑定图片图标。

在内容页面中有效的任何绑定图像,但在标题上无效。

这段语法来自我的 Tabbed Main Page .. 它调用了 City Tab Page。

 <NavigationPage Title="City" Icon="city.png">
    <x:Arguments>
      <local:CityPage />     
    </x:Arguments>
  </NavigationPage>

在 city.xaml.cs - 我正在调用这个方法。

 public async void GetWeather()
        {
            Weather weather = await Core.GetWeather("07004");
            if (weather != null)
            {
                this.BindingContext = weather;

                string sWeather = String.Format("{0}°F", weather.Temperature);

                this.Title = weather.Title + " " + sWeather;             
            }

        }

图像图标必须出现在温度的末尾。

【问题讨论】:

  • 请发布您的 xaml/代码的相关部分,以便我们看到您已经在做什么

标签: xamarin xamarin.forms openweathermap


【解决方案1】:

使用StringFormat

<Label Text={Binding Value, StringFormat='{0}° F'} ... />

【讨论】:

  • 这有助于.. 修复学位和 F。我需要获取 Image Icon.. Display Icon 变量,我已经评论过.. 我需要显示
【解决方案2】:

您应该能够将 url 绑定到图像

<ContentView Style="{StaticResource fieldView}">
  <StackLayout Orientation="Horizontal">
    <Label  x:Name="tempLabel" Text="{Binding Temperature}" Style="{StaticResource fieldStyle}" />
    <Image Source="{Binding DisplayIcon}" />
  </StackLayout>
</ContentView>

【讨论】:

  • 谢谢 Jason ... this.Title = txtLoc.Text + " " + tempLabel.Text + imgIcon; ImgIcon 是我的 ImageSource 名称。我需要将图像与我的字符串连接起来..我的语法不起作用..
  • 您不能将图像添加到标准标签。使用 StackLayout 将标签和图像组合在一起。
  • 我在我的 xaml 语法和后面的代码中提到了相同的内容:this.Title = txtLoc.Text + " " + tempLabel.Text; - 但是,我无法获得温度和图像
  • 如果您想显示您的代码,请编辑您的问题。编辑我的回复以包含您的非工作代码只是令人困惑。您建议的编辑包含一个带有多个子项的 ContentView。一个 ContentView 只能有一个子元素 - 如果您想要多个元素,您需要将它们包含在 StackLayout 或其他容器中。
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 2020-09-09
  • 2017-10-25
  • 2017-10-23
  • 1970-01-01
相关资源
最近更新 更多