XAML 根据另一个对象的id从该对象获取所有对象

ukdjmx9f  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(136)

我有两门课

    • 条款**
public class Terms
    {
        [PrimaryKey, AutoIncrement]
        public int TermId { get; set; }
        public string TermName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public DateTime RealStart => DateTime.Parse(StartDate);
        public DateTime RealEnd => DateTime.Parse(EndDate);
    }
    • 课程**
public class Courses
    {
        [PrimaryKey, AutoIncrement]
        public int CourseId { get; set; }
        public int TermId { get; set; }
        public string CourseName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public DateTime RealStart => DateTime.Parse(StartDate);
        public DateTime RealEnd => DateTime.Parse(EndDate);
        public string CIName { get; set; }
        public string CIEmail { get; set; }
        public string CIPhone { get; set; }
        public string Status { get; set; }
        public bool Notifications { get; set; }
        public string Notes { get; set; }
    }

第1学期-课程1课程2
在第1学期选择课程时,我尝试显示该课程的扩展课程详细信息。

    • xaml**
<CollectionView x:Name="CourseCollection"
                                        ItemsSource="{Binding Courses}"
                                        EmptyView="No Courses to display"
                                        SelectionMode="Single"
                                        SelectionChanged="CourseCollection_SelectionChanged">
    • C#**
private async void CourseCollection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var course = (Courses)e.CurrentSelection.FirstOrDefault();
            if (e.CurrentSelection != null)
            {
                await Navigation.PushAsync(new CoursesDetail(course));
            }
        }

它显示了两条航线

    • 课程详细信息**
    • xaml**
<CollectionView x:Name="CoursesCollection"
                                        ItemsSource="{Binding Courses}"
                                        EmptyView="No courses to view"
                                        SelectionMode="Single">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Grid Padding="5" RowSpacing="1" ColumnSpacing="10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <Label Grid.Row="0" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course ID</Label>
                                <Label Text="{Binding CourseId}" Grid.Row="0" Grid.Column="1" FontSize="Medium" x:Name="CourseId" ></Label>

                                <Label Grid.Row="1" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Term ID</Label>
                                <Label  Text="{Binding TermId}" Grid.Row="1" Grid.Column="1" FontSize="Medium" x:Name="TermId"></Label>

                                <Label Grid.Row="2" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course Name</Label>
                                <Label x:Name="CourseName"  Grid.Row="2" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CourseName}"></Label>

                                <Label Grid.Row="3" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Start Date</Label>
                                <Label Grid.Row="3" Grid.Column="1" FontSize="Medium" x:Name="StartDate" Text="{Binding StartDate}"></Label>

                                <Label Grid.Row="4" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">End Date</Label>
                                <Label Grid.Row="4" Grid.Column="1" FontSize="Medium" x:Name="EndDate" Text="{Binding EndDate}"></Label>

                                <Label Grid.Row="5" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Name</Label>
                                <Label x:Name="CIName" Grid.Row="5" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIName}"></Label>

                                <Label Grid.Row="6" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Email</Label>
                                <Label x:Name="CIEmail" Grid.Row="6" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIEmail}"></Label>

                                <Label Grid.Row="7" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Phone</Label>
                                <Label x:Name="CIPhone" Grid.Row="7" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIPhone}"></Label>

                                <Label Grid.Row="8" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course Status</Label>
                                <Label Grid.Row="8" Grid.Column="1" x:Name="Status" FontSize="Medium" Text="{Binding Status}"></Label>

                                <Label Grid.Row="9" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Notes</Label>
                                <Label x:Name="Notes" Grid.Row="9" Grid.Column="1" FontSize="Medium" Text="{Binding Notes}"></Label>

                                <Label Grid.Row="10" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Notifications</Label>
                                <Label x:Name="Notifications" Grid.Row="10" Grid.Column="1" FontSize="Medium" Text="{Binding Notifications}"></Label>
                            </Grid>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
    • c#**
public partial class CoursesDetail : ContentPage
    {
        private readonly int _selectedCourseId;
        private Courses theBiggestCourses;

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            CoursesCollection.ItemsSource = await database.GetCourses(_selectedCourseId);
            AssessmentsCollection.ItemsSource = await database.GetAssessments(_selectedCourseId);
        }

        public CoursesDetail()
        {
            InitializeComponent();
        }

        public CoursesDetail(Courses courses)
        {
            _selectedCourseId = courses.CourseId;
            InitializeComponent();
            this.theBiggestCourses = courses;
        }

我试过手动通过构造函数传递每个文本字段,我有一些应该自动填充的示例数据,并认为这是问题所在,但我删除了这些方法,并尝试手动添加一个术语,该术语的课程,但只要它超过1门课程,它就会崩溃。
编辑--我的豌豆大小的大脑理论可能是,由于Terms是作为一个对象传递的,捕获了该对象的所有元素,它仍然保留着该Term对象,所以当调用Courses对象时,它捕获了该terms对象的所有courses对象。我希望我解释得很好。
编辑2-获取课程

public static async Task<IEnumerable<Courses>> GetCourses(int termId)
        {
            await Init();

            var courses = await db.Table<Courses>()
                .Where(i => i.TermId == termId).ToListAsync();

            return courses;
        }

        public static async Task<IEnumerable<Courses>> GetCourses()
        {
            await Init();

            var courses = await db.Table<Courses>().ToListAsync();

            return courses;
        }

这是GetCourses的两个参数,是因为我们在这里传递了一个int型的termId,在方法中传递了一个object,然后默认为非参数化的构造函数,它会返回所有的参数吗?

r1zk6ea1

r1zk6ea11#

GetCourses需要一个TermID

public static async Task<IEnumerable<Courses>> GetCourses(int termId)
    {
        await Init();

        var courses = await db.Table<Courses>()
            .Where(i => i.TermId == termId).ToListAsync();

        return courses;
    }

但调用它时传递的是CourseID

CoursesCollection.ItemsSource = await database.GetCourses(_selectedCourseId);

相关问题