2017-08-01 54 views
0

我试图在ListView组件中显示两个TextCell。五月MainPage.xaml代码:无法显示两个TextCell

<StackLayout> 

    <Button Text="Go To Map Page" Clicked="Button_OnClicked"/> 

    <ListView x:Name="MyList"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextCell Text="{Binding Name}" /> 
       <TextCell Text="{Binding Age}" /> 
      </DataTemplate> 

     </ListView.ItemTemplate> 

    </ListView> 

</StackLayout> 

CS代码:

namespace App20 
{ 
    public partial class MainPage : ContentPage 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
      MyList.ItemsSource = new List<Person> 
      { 
       new Person() 
       { 
        Name = "Dima", 
        Age = 20 
       }, 
       new Person() 
       { 
        Name = "Roma", 
        Age = 21 
       }, 
       new Person() 
       { 
        Name = "Masha", 
        Age = 19 
       } 


      }; 
     } 

     private async void Button_OnClicked(object sender, EventArgs e) 
     { 
      await Navigation.PushAsync(new MapPage()); 
     } 
    } 
} 

如果我只显示元素之一(姓名,年龄),一切工作正常。如果我试图显示他们两个我的应用程序挂起。

第二个问题。为什么只有TextCell有效?我试图用ButtonLabel,并得到例外。

下面是截图:

enter image description here

enter image description here

+1

创建自己的自定义单元格的布局,我认为这是一个重复:https://stackoverflow.com/questions/25052746/xamarin-forms-binding-multiple-textcells-in-one-listview – cotnic

+0

你必须为你的数据模板使用一个容器视图,例如'StackLayout'或'Grid' – Milen

回答

1

你不能在一个ListView控件使用两个单元。您可以使用TextCell的详细属性来显示文本

<DataTemplate> 
    <TextCell Text="{Binding Name}" Detail="{Binding Age}" /> 
    </DataTemplate> 

的2号线,也可以使用ViewCell