如何从另一个页面的 BindingContext 添加数据到 CollectionView

How to add data to CollectionView from BindingContext from another page

我在 MainPage 下面有这个点击按钮,它工作正常,如果我将数据传递(绑定)到第二页(CourseDetailViewPage)中的标签,我可以看到显示的数据;

但现在我想在位于第二页 (CourseDetailPage) 的集合视图中显示数据。

//Click button in MainPage
        public async void OnOkGetCourseButton(object sender, EventArgs e)
        {
            var inputtedCourseNumber = ctrlEntryTextBox.Text;

            if(inputtedCourseNumber == string.Empty)
            {
                await DisplayAlert("Note", "\nPlease enter your Course number", "OK");
            }
            else
            {
                try
                {

                    CourseService CourseService = new CourseService();
                    var data = await CourseService.getCourses();

                    var userCourse = data.Where(x => x.Id.ToString() == inputtedCourseNumber).FirstOrDefault();

                    UserDialogs.Instance.HideLoading();

                    if (userCourse == null)
                    {
                        await DisplayAlert("Note", "\nCourse number does not exist. Verify your Course number", "OK");
                    }
                    else
                    {
                        await DisplayAlert("Note", "\nOurahh - Course number found !!!", "OK");

                        await DisplayAlert("Note", "\n" + userCourse.Id + "\n" + userCourse.CourseDetail + "\n" + userCourse.IsCourseComplete + "\n" + userCourse.CourseDate, "OK");



                        await Navigation.PushAsync(new CourseDetailPage
                        {
                            BindingContext = userCourse as Course
                        });


                    }
                }
                catch (Exception)
                {
                    await DisplayAlert("Error", "Error Network", "OK");
                }
            }
        }
    

======

// CourseDetailPage (Second page)
        
        <CollectionView x:Name="myCollection" ItemsSource="{Binding userCourse}" 
                            HeightRequest="200">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid RowDefinitions="Auto,Auto,Auto">
                    <Label Grid.Row="0" Text="{Binding Id}"/>
                    <Label Grid.Row="0" Text="{Binding CourseDetail}"/>
                    <Label Grid.Row="0" Text="{Binding IsCourseComplete}"/>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView> 

改变

await Navigation.PushAsync(new CourseDetailPage
{
    BindingContext = userCourse as Course
});

ObservableCollection<Course> courses = new ObservableCollection<Course>();
courses.Add(userCourse);
await Navigation.PushAsync(new CourseDetailPage
{
    BindingContext = courses
});

原因:您需要将 集合 传递给 ItemsSource。在这种简单的情况下,它不必是一个“可观察的”集合,但在更动态的用途中,代码 adds/removes 元素显示项目列表时,这可能是您想要的.所以我就用在这里,作为一个好习惯。

改变

<CollectionView x:Name="myCollection" ItemsSource="{Binding userCourse}" 
                HeightRequest="200">

<CollectionView x:Name="myCollection" ItemsSource="{Binding .}" 
                HeightRequest="200">

原因:在第一个代码更改中,我们将新 CourseDetailPage 的 BindingContext 设置为我们要用作 ItemsSource 的集合。 “{Binding .}”是一种引用 BindingContext 本身的方法。在这种情况下,BindingContext 用作 ItemsSource 的列表。


注意:虽然上面的方法应该有效,但它是有限的。它不提供任何方式将其他值传递给 CourseDetailPage 的其他部分。如果你发现要传递userCourse和一些其他信息,那就稍微复杂一些。

定义一个 class,它有一个 public 属性 Courses,并传递那个 [=48] 的一个实例=] 作为 BindingContext。那么 ItemsSource 将是 {Binding Courses}:

public class Details
{
    public property ObservableCollection<Course> Courses { get; set; }
    public property int Prop2 { get; set; }
}

...
ObservableCollection<Course> courses = new ObservableCollection<Course>();
courses.Add(userCourse);
var details = new Details { Courses = courses, Prop2 = 123 };

await Navigation.PushAsync(new CourseDetailPage
{
    BindingContext = details 
});

...
<CollectionView x:Name="myCollection" ItemsSource="{Binding Courses}" 
                HeightRequest="200">

... some other view that uses `..="{Binding Prop2}"` ...

这是数据的“单向”显示。如果用户需要 与数据交互 ,请查看 MVVM.