在(2)
之后不知道如何动态处理这种情况
1)应用程序调用REST Json Web服务
2)反序列化Json响应
3)将数据绑定到ListView
下面是工作示例。
问题:我需要在每行记录中添加一个按钮。例如,如何在Johnny Depp旁边添加一个按钮?为此按钮添加EventHandler?
这意味着我需要动态生成XAML?如何绑定?
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 :(得分:0)
您已经创建了自定义单元格,使用Command和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
});
}
}