简体   繁体   English

Xamarin.Forms填充列表视图

[英]Xamarin.Forms fill a listview

I'm trying to create a simple page with a ListView and a Button "Add", which simply adds another item to the list with a count and a timestamp. 我正在尝试创建一个具有ListView和“添加” Button的简单页面,该Button只是将另一个带有计数和时间戳的项目添加到列表中。

App.cs App.cs

namespace App1
{
    public class App : Application
    {
        public App()
        {
            // The root page of your application
            MainPage = new NavigationPage(new Page1());
        }
    }
}

As you can see, I created a Page.xaml and Page.xaml.cs . 如您所见,我创建了一个Page.xamlPage.xaml.cs

Page.xaml Page.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.Page1">
  <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand">
    <Label Text="Inizio" VerticalOptions="Start"/>
    <ListView VerticalOptions="FillAndExpand" x:Name ="list">
    </ListView>
    <Label Text="Fine" VerticalOptions="End"/>
  </StackLayout>
  <ContentPage.ToolbarItems>
    <ToolbarItem x:Name="add">
      <ToolbarItem.Icon>
        <OnPlatform
          x:TypeArguments="FileImageSource"
          iOS="appbar.add.png"
          Android="appbar.add.png"
          WinPhone="appbar.add.png"
        />
      </ToolbarItem.Icon>
    </ToolbarItem>
  </ContentPage.ToolbarItems>
</ContentPage>

Page.xaml.cs Page.xaml.cs

namespace App1
{
    public partial class Page1 : ContentPage
    {
        int count = 0;
        ObservableCollection<TextCell> cells = new ObservableCollection<TextCell>();

        public Page1()
        {
            InitializeComponent();
            //this.ToolbarItems.Add(new ToolbarItem { Text = "Add" });
            add.Clicked += Add_Clicked;
        }

        private void Add_Clicked(object sender, EventArgs e)
        {
            cells.Add(new TextCell { Text = count.ToString(), Detail = DateTime.Now.ToString("dd/MM/yyyy --> hh:mm:ss") });
            list.ItemsSource = cells; 
            count++;
        }
    }
}

Now, everything works, but the problem is the List . 现在,一切正常,但问题是List I have the List filled with TextCells , and each of them has the property I set. 我的List充满了TextCells ,并且每个都有我设置的属性。 However, the List is filled by elements with only the text "Xamarin.Forms.TextCell". 但是, List由仅包含文本“ Xamarin.Forms.TextCell”的元素填充。

This is my first attempt with Xamarin, what am I doing wrong? 这是我第一次尝试使用Xamarin,我在做什么错?

You have to define an item template 您必须定义一个项目模板

This is an example of Xamarin documentation 这是Xamarin文档的示例

class Person
{
  public string FullName
  {
    get;
    set;
  }

  public string Address
  {
    get;
    set;
  }
}

void SetupView()
{
  var template = new DataTemplate (typeof (TextCell));

  // We can set data bindings to our supplied objects.
  template.SetBinding (TextCell.TextProperty, "FullName");
  template.SetBinding (TextCell.DetailProperty, "Address");

  // We can also set values that will apply to each item.
  template.SetValue (TextCell.TextColorProperty, Color.Red);

  itemsView.ItemTemplate = template;
  itemsView.ItemsSource = new[] {
    new Person { FullName = "James Smith", Address = "404 Nowhere Street" },
    new Person { FullName = "John Doe", Address = "404 Nowhere Ave" }
  };
}

Though above answer fills up the conceptual gap in your implementation (+1 for that), but it doesn't give you idea of general solution to the problem that you want to solve . 尽管以上答案填补了您实现中的概念空白(为此+1),但是它并没有给您解决您要解决的问题的一般解决方案。 Kindly find the whole solution below . 请在下面找到整个解决方案。

This is your CodeBehindFile of Page class . 这是您的Page类的CodeBehindFile。

namespace stackQues
{
    public partial class Page : ContentPage
    {
        int count = 0;
        List<TextCellItem> data = new MenuData ();
        public Page()
        {
            InitializeComponent();
            //this.ToolbarItems.Add(new ToolbarItem { Text = "Add" });

        }

        private void Add_Clicked(object sender, EventArgs e)
        {
            //cells.Add(new TextCell { Text = count.ToString(), Detail = DateTime.Now.ToString("dd/MM/yyyy --> hh:mm:ss") });


            data.Add (new TextCellItem {
                TextLabel = count.ToString (),
                DetailTextLabel = DateTime.Now.ToString ("dd/MM/yyyy --> hh:mm:ss")
            });
            count++;
            list.ItemsSource = data;
            list.ItemTemplate = new DataTemplate(typeof(YourTextCell));

        }
    }
}

Here MenuData is 这里的MenuData是

namespace stackQues
{
    class MenuData: List<TextCellItem>
    {
        public MenuData()
        {
        }
    }
}

TextCellItem is as follows TextCellItem如下

namespace stackQues
{
    public class TextCellItem
    {
        public string TextLabel { get; set;}
        public string DetailTextLabel { get; set;}
    }
}

YourTextCell is as follows YourTextCell如下

namespace stackQues
{
    class YourTextCell:TextCell
    {
        public YourTextCell() {
            this.SetBinding (TextCell.TextProperty, "TextLabel");
            this.SetBinding (TextCell.DetailProperty, "DetailTextLabel");
        }
    }
}

My Xaml markup is little different but intended for some purpose 我的Xaml标记几乎没有什么不同,但出于某些目的

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="stackQues.Page">
<StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand">
    <Label Text="Inizio" VerticalOptions="Start"/>
    <ListView VerticalOptions="FillAndExpand" x:Name ="list">
    </ListView>
    <Label Text="Fine" VerticalOptions="End"/>
    <Button Text = "add" Clicked = "Add_Clicked" ></Button>
  </StackLayout>
<!--  <ContentPage.ToolbarItems>
    <ToolbarItem x:Name="add">
      <ToolbarItem.Icon>
        <OnPlatform
          x:TypeArguments="FileImageSource"
          iOS="appbar.add.png"
          Android="cross.png"
          WinPhone="appbar.add.png"
        />
      </ToolbarItem.Icon>
    </ToolbarItem>
  </ContentPage.ToolbarItems>-->
</ContentPage>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM