【问题标题】:Can't display two TextCell无法显示两个 TextCell
【发布时间】:2018-01-08 06:36:03
【问题描述】:

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

<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 并得到了例外。

这里是截图:

【问题讨论】:

标签: c# android xamarin


【解决方案1】:

您不能在一个 ListView 中使用两个单元格。您可以使用 TextCell 的 Detail 属性显示第二行文本

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

或者您可以使用 ViewCell 创建自己的自定义单元格布局

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2019-12-29
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多