【问题标题】:Xamarin Forms ListView not displaying TextXamarin Forms ListView 不显示文本
【发布时间】:2018-01-21 03:37:06
【问题描述】:

对于我的应用,我想要一个非常简单的列表视图,所以在我的 VS 中,我右键单击 -> 添加新项目 -> 列表视图页面。我没有更改 .xaml 或 .cs 中的任何内容

ListView.xaml

<ListView ItemsSource="{Binding Items}"
        ItemTapped="Handle_ItemTapped"
        CachingStrategy="RecycleElement">

  <!--Built in Cells-->
  <ListView.ItemTemplate>
    <DataTemplate>
      <TextCell />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

ListView.xaml.cs

public ObservableCollection<string> Items { get; set; }

    public FacilityListView()
    {
        InitializeComponent();

        Items = new ObservableCollection<string>
        {
            "Item 1",
            "Item 2",
            "Item 3",
            "Item 4",
            "Item 5"
        };

        BindingContext = this;
    }

    async void Handle_ItemTapped(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null)
            return;

        await DisplayAlert("Item Tapped", "An item was tapped.", "OK");

        //Deselect Item
        ((ListView)sender).SelectedItem = null;
    }

列表只显示单元格,没有文字

我尝试更改绑定,添加 textColor 等,但无论如何,单元格仍然为空

【问题讨论】:

    标签: c# xaml listview xamarin.forms


    【解决方案1】:

    您的模板只包含一个空的 TextCell。你必须使用绑定来告诉它要显示什么

    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell Text="{Binding .}" />
      </DataTemplate>
    </ListView.ItemTemplate>
    

    【讨论】:

    • 哇,非常感谢,我没想到。我很困惑,因为我假设模板可以开箱即用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2020-07-28
    相关资源
    最近更新 更多