2017-10-06 17 views
0

私は自分の街の現在の天気を見ることができるXamarinで簡単なアプリを試しています。しかし、私は右にそれを得ることができないようです..私は私の都市の座標を検索しているURLで、openweathermap.orgからAPIを持っている。 私の "OpenWeather"クラスのクラスを取得するために、私はクラスを生成するためにjson2csharpを使いました。XamarinはWeb APIからデータを取得します

都市名、天気予報、座標を表示したいと思います。

MainPage.xaml.cs:

public partial class MainPage : ContentPage 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     GetWeather(); 

    } 

    public async void GetWeather() 
    { 

     try 
     { 
      string URL = "http://api.openweathermap.org/data/2.5/weather?lat=58.34&lon=11.94&appid=((((((MY API KEY))))))))))))"; 

      HttpClient httpClient = new HttpClient(); 
      HttpResponseMessage response = await httpClient.GetAsync(new Uri(URL)); 

      if (response.IsSuccessStatusCode) 
      { 
       var content = await response.Content.ReadAsStringAsync(); 
       var weatherList = JsonConvert.DeserializeObject<RootObject>(content); 

       //Databind the list 
       lstWeather.ItemsSource = weatherList; 
      } 
     } 
     catch (Exception ex) 
     { 
      //ToDo Give errormessage to user and possibly log error 
      System.Diagnostics.Debug.WriteLine(ex.ToString()); 
     } 
    } 
} 

MainPage.xamlを:

<ProgressBar IsEnabled="True"></ProgressBar> 
<ListView x:Name="lstWeather"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
       <StackLayout> 
        <StackLayout Orientation="Vertical">--> 
         <Label Text="{Binding name}" /> 
         <Label Text="{Binding desciption}" Margin="20,0,0,0"/> 
         <Label Text="{Binding lon}" Margin="20,0,0,0"/> 
         <Label Text="{Binding lat}" Margin="20,0,0,0"/> 
        </StackLayout> 
       </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 


namespace TestApp 

{ パブリッククラスCOORD {

私は、私がこれまでに得たコードを投稿します public double lon {get;セット; } パブリックdouble lat {get;セット; } }

public class Weather 
{ 
    public int id { get; set; } 
    public string main { get; set; } 
    public string description { get; set; } 
    public string icon { get; set; } 
} 

public class Main 
{ 
    public double temp { get; set; } 
    public int pressure { get; set; } 
    public int humidity { get; set; } 
    public double temp_min { get; set; } 
    public double temp_max { get; set; } 
} 

public class Wind 
{ 
    public double speed { get; set; } 
    public int deg { get; set; } 
} 

public class Clouds 
{ 
    public int all { get; set; } 
} 

public class Sys 
{ 
    public int type { get; set; } 
    public int id { get; set; } 
    public double message { get; set; } 
    public string country { get; set; } 
    public int sunrise { get; set; } 
    public int sunset { get; set; } 
} 

public class RootObject 
{ 
    public Coord coord { get; set; } 
    public List<Weather> weather { get; set; } 
    public string @base { get; set; } 
    public Main main { get; set; } 
    public int visibility { get; set; } 
    public Wind wind { get; set; } 
    public Clouds clouds { get; set; } 
    public int dt { get; set; } 
    public Sys sys { get; set; } 
    public int id { get; set; } 
    public string name { get; set; } 
    public int cod { get; set; } 
} 
+1

"取得できないようですそれは正しい "あなたの問題の有用な説明ではありません。具体的に何が問題になっていますか?エラーまたは例外が発生していますか? – Jason

+0

@Jason Oh申し訳ありません!私の例外は、私のMainPage.xaml.csファイルで、errormessageは次のようになっています。「Impleicity can not type 'TestApp.RootObject'を 'System.Collections.IEnumerable'に変換します。明示的な変換が存在します(キャストがありませんか? この問題が修正されたときに、自分の文法が正しいものを印刷できるかどうかはわかりません。 –

答えて

1

問題はここにある:

lstWeather.ItemsSource = weatherList; 

weatherListRootObjectですが、ListViewはおそらく、これやりたいことのDataSource

ためIEnumerable必要があります。

lstWeather.ItemsSource = weatherList.weather; 
+0

これは私のエラーを取り除くのに役立ちました。私のマウスでマウスを動かすとそこにリストアイテムがあることがわかります。lstWeather.ItemsSource = weatherList.weatherにブレークポイントを置くと、私は見たいオブジェクトを見ることができます。 xamlでのデータバインディングが間違っていますか? –

+0

あなたのリスト内の個々のアイテムは天気オブジェクトなので、バインディングは天気のプロパティである必要があります。 – Jason

+0

まあ、私は出力ウィンドウでこれを取得します.. https://imgur.com/a/n4qZF これでデータバインディングはどのようになりますか?

関連する問題