2017-06-21 43 views
0

有沒有線索,如何處理這種情況下動態(2)之後
1)應用程序調用REST JSON網絡服務
2)反序列化JSON響應
3)將數據綁定到ListView
如何動態地生成網格佈局包含按鈕和數據

下面是工作示例。

問題:我需要爲記錄的每一行添加一個按鈕。例如,如何在Johnny Depp旁邊添加一個按鈕?爲此按鈕添加EventHandler?

這意味着我需要動態生成XAML?如何綁定?

enter image description here

here to code to handle the Returned Json Result: 

    -- Model: 

    public class Phone 
     { 
      public string mobile { get; set; } 
      public string home { get; set; } 
      public string office { get; set; } 
     } 

     public class Contact 
     { 
      public string id { get; set; } 
      public string name { get; set; } 
      public string email { get; set; } 
      public string address { get; set; } 
      public string gender { get; set; } 
      public Phone phone { get; set; } 
     } 

     public class ContactList 
     { 
      public List<Contact> contacts { get; set; } 
     } 


---- Call REST 
var client = new System.Net.Http.HttpClient();     
var response = await client.GetAsync("http://Rest api");     
string contactsJson = response.Content.ReadAsStringAsync().Result;     
ContactList ObjContactList = new ContactList(); 

if (contactsJson != "")   
{   
ObjContactList = JsonConvert.DeserializeObject<ContactList>(contactsJson); 

} 
listviewConacts.ItemsSource = ObjContactList.contacts; 



    ------XAML: 

    <Grid> 
     <Grid> 
     <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 

      <Label Grid.Row="0" Margin="10" Text="Display Json Data" FontSize="25" /> 

      <ListView x:Name="listviewConacts" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected"> 

      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
        <Grid HorizontalOptions="FillAndExpand" Padding="10"> 
         <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
       <Label Text="{Binding name}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/> 
       <Label Text="{Binding email}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange" FontAttributes="Bold"/> 
       <Label Text="{Binding phone.mobile}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/> 

       <BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" /> 
        </Grid> 
       </ViewCell> 

      </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 
     <ActivityIndicator x:Name="ProgressLoader" IsRunning="True"/> 
    </Grid> 

感謝

回答

0

您創建自定義單元格已經,添加按鈕將它與命令和CommandParameter。您viewcell網格內添加

<Button Text="Toggle" Command="{Binding Source={x:Reference YourPage}, Path=BindingContext.TapCommand}" CommandParameter="{Binding}"/> 

然後在模型

public Command TapCommand 
     { 
      get 
      { 
       return new Command((obj) => 
       { 
        //obj here will be your data record 
       }); 
      } 
     } 
+0

說,我需要一個按鈕啓動彈出窗口或導航到另一個頁面。此按鈕會將Johnny Depp數據傳遞給彈出窗口或下一頁以進行編輯。你能告訴我如何基於我的例子嗎? – MilkBottle

+0

編輯我的回答 –

+0

它適合你嗎? –