2016-12-25 79 views
0

我正在使用XAMARIN应用程序。我打电话打开天气API,并能够获得JSON响应。如何通过调用Openweather API在xamarin中显示天气图标?

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

仍然我不能绑定图像图标。

无论绑定图像在内容页面中,还是在标题上都不起作用。

这段语法来自我的标签主页..它调用城市标签页。

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

     } 

enter image description here

图片图标具有来在该温度下的端部。

+0

请发表您的XAML /代码中的相关部分,所以我们可以看到你已经在做什么 – Jason

回答

2

使用StringFormat

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

这有助于..修复度和F.我需要得到的图像图标..显示图标变量,我已评论..我需要显示 – goofyui

1

你应该能够网址绑定到的图像

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

谢谢贾森... this.Title = txtLoc.Text +“ “+ tempLabel.Text + imgIcon; ImgIcon是我的ImageSource的名称。我需要连接图像和我的字符串..我的语法不起作用.. – goofyui

+0

您无法将图像添加到标准标签。使用StackLayout将标签和图像彼此相邻。 – Jason

+0

我在xaml语法和后面的代码中提到了相同的内容:this.Title = txtLoc.Text +“”+ tempLabel.Text; - 但是,如果您想显示您的代码,我无法获取温度和图像 – goofyui