如何基于XAML上的列表向ListView动态添加项?

qcbq4gxm  于 2022-12-31  发布在  其他
关注(0)|答案(2)|浏览(144)

我正在尝试用XAML创建一个<ListView>。列表应该显示类Task的列表的内容。

taskView.ItemsSource = company.tasks;

我正在使用的XAML版本是MAUI上考虑的版本,因此大多数教程显示的元素不包括在所使用的XAML版本中。
我试着这样做:

<ListView x:Name="taskView"
      Margin="120,0,0,60"
      ItemsSource="{Binding tasks}"
      ItemSelected="taskView_SelectionChanged">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding name}"/>
                <TextCell Text="{Binding status}"/>
                <TextCell Text="{Binding dedaline}"/>
                <TextCell Text="{Binding description}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

但它没有工作,它既不会打开应用程序,也不会抛出异常。
如果我只留下一个<TextCell>,它会打开并显示(只有名称)。如何显示更多细节?
下面是类Task

public class Task
    {
        public Task(string name, List<string> departments, Status status, DateOnly deadline, string description)
        {
            this.name = name;
            this.departments = departments;
            this.status = status;
            this.deadline = deadline;
            this.description = description;
        }

        public string name { get; private set; }
        public List<string> departments { get; private set; } = new List<string>();
        public Status status { get; private set; }
        public DateOnly deadline { get; private set; }
        public Employee? author { get; set; }
        public string description { get; private set; }
        public List<Employee> employees { get; private set; } = new List<Employee>();
    }

我是XAML的新手,非常感谢您的帮助。谢谢。

byqmnocz

byqmnocz1#

首先,一件不相关的事情。类“任务”可能会给你带来问题,因为你已经在Iternet中有了System.Threading.Task
正如@Jason所说,您不应该为了同一个目的将ListView(或CollectionView)绑定到一个源两次。

XAML文件

<ContentPage
    x:Class="MauiApp.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:models="clr-namespace:MauiApp.Models"
    xmlns:views="clr-namespace:MauiApp.Views"
    x:DataType="views:MainPage">

    <CollectionView            
        ItemsSource="{Binding Tasks}">

        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="models:Task">
                <VerticalStackLayout Margin="15">
                    <Entry Text="{Binding name}" />
                    <Entry Text="{Binding status}" />
                    <Entry Text="{Binding deadline}" />
                    <Entry Text="{Binding description}" />
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>

    </CollectionView>

</ContentPage>

检查XML命名空间(xmlns)。我假设您的任务模型在“Models”文件夹中,视图在“Views”文件夹中。您需要此导入,因为您的内容页面的数据类型与MainPage绑定,但您的CollectionView与Task类绑定。
在DataTemplate中,您只能有一个项。因此,您应该选择一个容器项,如本答案中的VerticalStackLayout

类文件

public partial class MainPage : ContentPage
{
    public ObservableCollection<Task> Tasks { get; set; } = new ObservableCollection<Task>();

    public MainPage()
    {
        InitializeComponent();
        BindingContext = this;

        Tasks.Add(new Task("task 1", new List<string>() { "dep1", "dep2" }, "status 1", DateOnly.MinValue, "description 1"));
        Tasks.Add(new Task("task 2", new List<string>() { "dep3", "dep4" }, "status 2", DateOnly.MinValue, "description 2"));
    }
}

在类中,可以使用ObservableCollection,并且必须将Context与BindingContext = this;绑定
如果您使用MVVM,那么您可以将其绑定到您的ViewModel。
这应按预期工作,但如果需要,您可能需要对OnPropertyChanged进行类化。

xzlaal3s

xzlaal3s2#

ListViewDataTemplate只能包含一个单个子元素。如果要具有多个数据元素,请使用ViewCell而不是TextCell,并生成您自己的自定义布局

<ViewCell>
   <StackLayout BackgroundColor="#eee"
                    Orientation="Vertical">
     <StackLayout Orientation="Horizontal">
        <Image Source="{Binding image}" />
        <Label Text="{Binding title}"
                            TextColor="#f35e20" />
        <Label Text="{Binding subtitle}" HorizontalOptions="EndAndExpand"
                            TextColor="#503026" />
      </StackLayout>
    </StackLayout>
   </ViewCell>

相关问题