我无法通过 ListView(Xamarin.Forms) 显示员工的姓名、任务和日期

I could not display the employee's name, tasks, and date via the ListView(Xamarin.Forms)

我做了两张表,第一张是员工的名字,第二张是职责和日期。 但是我无法显示员工的姓名、分配给他的职责和日期。 这是我的代码,希望你能帮助我。

public class MyTabels
{
    [Table("Employee")]
    public class Employee
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        public string Name { get; set; }
        public string LastName { get; set; }

        [OneToMany]
        public List<Duty> Duties { get; set; }
    }


    [Table("Duties")]
    public class Duty
    {

        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        public string Description { get; set; }

        public DateTime Deadline { get; set; }

        [ForeignKey(typeof(Employee))]
        public int EmployeeId { get; set; }
    }
}

这个 XAML 有一个 CollectionView,我已经向其中添加了项目:

<CollectionView x:Name="CV" IsGrouped="True">
            <CollectionView.HeaderTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                        <Label Text="{Binding Name}" FontSize="Large"/>
                        <Label Text="{Binding LastName}" FontSize="Large"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </CollectionView.HeaderTemplate>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Description}" FontSize="Medium"/>
                            <Label Text="{Binding Deadline}" FontSize="Medium"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

这是我的代码:

var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
        var db = new SQLiteConnection(dbpath);
        db.CreateTable<Employee>();
        db.CreateTable<Duty>();
var employee = new Employee
        {
            Name = "Andrew",
            LastName = "Programmer"
        };
        db.Insert(employee);


        var duty1 = new Duty()
        {
            Description = "Project A Management",
            Deadline = new DateTime(2017, 10, 31)
        };

        var duty2 = new Duty()
        {
            Description = "Reporting work time",
            Deadline = new DateTime(2022, 12, 31)
        };

        db.Insert(duty1);
        db.Insert(duty2);

        employee.Duties = new List<Duty> { duty1, duty2 };
        db.UpdateWithChildren(employee);

        var employeeStored = db.GetWithChildren<Employee>(employee.Id);

        CV.ItemsSource = employeeStored.Duties;

制作一个新的 class 来转换您从数据库中获取的数据以匹配 CollectionView ItemSource。

型号:

public class EmployeeList : List<Duty>
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public EmployeeList(string name, string lastName, List<Duty> duties) : base(duties)
    {
        Name = name;
        LastName = lastName;
    }
}

用法:使用 GetAllWithChildren 从数据库中获取所有员工到列表中。

       public partial class Page3 : ContentPage
{
    public List<EmployeeList> employeeStored { get; set; } = new List<EmployeeList>();

    public Page3()
    {
        InitializeComponent();

        var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
        var db = new SQLiteConnection(dbpath);
        db.CreateTable<Employee>();
        db.CreateTable<Duty>();
        var employee = new Employee
        {
            Name = "Andrew",
            LastName = "Programmer"
        };
        db.Insert(employee);


        var duty1 = new Duty()
        {
            Description = "Project A Management",
            Deadline = new DateTime(2017, 10, 31)
        };

        var duty2 = new Duty()
        {
            Description = "Reporting work time",
            Deadline = new DateTime(2022, 12, 31)
        };

        db.Insert(duty1);
        db.Insert(duty2);

        employee.Duties = new List<Duty> { duty1, duty2 };
        db.UpdateWithChildren(employee);

        //var employeeStored2 = db.GetWithChildren<Employee>(employee.Id);

        var employeeStored = db.GetAllWithChildren<Employee>();

        
        var list = Convert(employeeStored);
        CV.ItemsSource = list;
        this.BindingContext = this;

    }
    public List<EmployeeList> Convert(List<Employee> employees)
    {
        foreach (var item in employees)
        {
            employeeStored.Add(new EmployeeList(item.Name, item.LastName, item.Duties));
        }
        return employeeStored;

    }


}

请注意,CollectionView 没有单元格的概念。所以在你的 Xaml.

中删除它

更新:

Xaml:

      <CollectionView  x:Name="CV" IsGrouped="True">
        <CollectionView.GroupHeaderTemplate>
            <DataTemplate>
               
                    <StackLayout>
                        <Label Text="{Binding  Name}" FontSize="Large"/>
                        <Label Text="{Binding LastName}" FontSize="Large"/>
                    </StackLayout>
                 
            </DataTemplate>
        </CollectionView.GroupHeaderTemplate>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label Text="{Binding Description}" FontSize="Medium"/>
                    <Label Text="{Binding Deadline}" FontSize="Medium"/>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>