gpt4 book ai didi

c# - Xamarin.Forms 填充 ListView

转载 作者:行者123 更新时间:2023-11-30 21:50:05 25 4
gpt4 key购买 nike

我正在尝试创建一个带有 ListViewButton“添加”的简单页面,它只是将另一个项目添加到列表中,并带有计数和时间戳.

App.cs

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

如您所见,我创建了一个 Page.xamlPage.xaml.cs

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

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++;
}
}
}

现在,一切正常,但问题是 List。我的 List 中填充了 TextCells,并且它们中的每一个都具有我设置的属性。但是,List 由仅包含文本“Xamarin.Forms.TextCell”的元素填充。

这是我第一次尝试使用 Xamarin,我做错了什么?

最佳答案

你必须定义一个项目模板

这是 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" }
};
}

关于c# - Xamarin.Forms 填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36546252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com