我无法通过 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;

创建一个新类,以将从数据库中获得的数据转换为与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>

相关内容

最新更新