2017-11-05 2 views
0

私は非同期タスクを経由してAPIからデータを取得しています:移入のstacklayout

public async Task<PostModel.Post[]> GetPostsAsync() 
    { 
     var client = new HttpClient(); 
     var uri = new Uri("link here"); 
     var response = await client.GetAsync(uri); 
     if (response.IsSuccessStatusCode) 
     { 
      var jsonString = await response.Content.ReadAsStringAsync(); 
      var rootObject = JsonConvert.DeserializeObject<PostModel.Post[]>(jsonString); 
      return rootObject; 
     } 
     else 
      return null; 
    } 

はまた、私は、単純な垂直ビューを作成しました:

<ContentPage.Content> 
    <ListView x:Name="MainView"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <StackLayout Orientation="Vertical" HorizontalOptions="Fill" VerticalOptions="Fill" BackgroundColor="WhiteSmoke"> 
         <Frame OutlineColor="Black"> 
          <Label x:Name="Title" Text="{Binding Name}"/> 
          <Image x:Name="Image" Source="{Binding path}" /> 
         </Frame> 
        </StackLayout> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</ContentPage.Content> 

は、表示するためのカスタムセルを書きましたデータ:

public CustomCell() 
    { 
     //instantiate items in views 
     var Image = new Image(); 
     var NameLabel = new Label(); 
     var horizontalLayout = new StackLayout() { BackgroundColor = Color.WhiteSmoke }; 

     //set bindings 
     NameLabel.SetBinding(Label.TextProperty, new Binding("Name")); 
     Image.SetBinding(Image.SourceProperty, new Binding("path")); 
     Image.HorizontalOptions = LayoutOptions.Center; 
     Image.HeightRequest = 600; 
     Image.WidthRequest = 600; 
     NameLabel.FontSize = 24; 

     //add views to the view hierarchy 
     horizontalLayout.Children.Add(NameLabel); 
     horizontalLayout.Children.Add(Image); 

     //add to parent view 
     View = horizontalLayout; 
    } 

これで、非同期タスクからレイアウトを動的に取り込みたいと思います。たとえば、タスクが6項目を取得した場合 - 各非同期タスク項目からのデータを含む6つのセルを作成します。どのように私はそれを行うことができますどのような提案?私は完全に緑色のxamarin.formsで、ちょうどそれを試し始めた。 OnAppearingで

答えて

1

は、ちょうどあなたのListView

MainView.ItemSource = await GetPostsAsync(); 
+0

作品、感謝のソースを設定します。私はforeachでそれをやろうとしましたが、ポストがついたテーブルが作成される前に呼び出されました。 – Wolwgang