Xamarin - 将数据从一页中的 CollectionView 传递到另一页

Xamarin - pass data from CollectionView in one page to another page

我想知道如何在用户单击 CollectionView 内的框架时将数据从我的 MainPage.xaml 中的一个 CollectionView 传递到另一个名为 DetailsPage 的页面,我希望你明白我的意思.

这是我的 MainPage.xaml:

<CollectionView x:Name="CVSubjects" Margin="0,-50,0,0" ItemsLayout="HorizontalList">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout Margin="20,0,0,0">
                            <Frame BackgroundColor="{Binding BackgroundColor}" WidthRequest="250" HeightRequest="180" HorizontalOptions="Center" CornerRadius="30">
                                <Frame.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                                </Frame.GestureRecognizers>
                                <StackLayout>
                                    <Frame BackgroundColor="white" HasShadow="False" WidthRequest="30" HorizontalOptions="Start" CornerRadius="50" HeightRequest="10">
                                        <StackLayout Orientation="Horizontal" HeightRequest="10">
                                            <Image Source="redstaricon.png" WidthRequest="14" Margin="-10,-2,0,0"></Image>
                                            <Label Text="{Binding Rating}" VerticalOptions="Center" Margin="0,-5,0,0" FontAttributes="Bold" TextColor="Black"></Label>
                                        </StackLayout>
                                    </Frame>
                                    <Label Text="{Binding Name}" FontSize="21" TextColor="White" FontAttributes="Bold" Padding="0,10,0,0" WidthRequest="250"></Label>
                                    <StackLayout Orientation="Horizontal">
                                        <Frame CornerRadius="100" 
                                       HeightRequest="55"
                                       WidthRequest="60"
                                       HorizontalOptions="Center"
                                       Padding="0"
                                       IsClippedToBounds="True"
                                           Margin="0,20,0,0"
                                         >
                                            <Image Source="{Binding ImageUrl}" 
                                        HorizontalOptions="Center"
                                        VerticalOptions="Center"
                                               Aspect="Fill"></Image>
                                        </Frame>
                                        <Label Text="Teacher" TextColor="White" WidthRequest="100" Margin="0,25,0,0">
                                        </Label>
                                        <Label Text="{Binding Teacher}" VerticalOptions="Center" TextColor="White" WidthRequest="200" FontAttributes="Bold" Margin="-100,45,0,0" FontSize="17">
                                        </Label>
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

这是 MainPage.xaml.cs:

 public partial class MainPage : ContentPage
    {
        
        ObservableCollection<Subjects> subjects;
        public MainPage()
        {
            InitializeComponent();
            subjects = new ObservableCollection<Subjects>() { new Subjects { Id = 1, Name = "UX - UI Design", Teacher = "Gustavo Kenter", Rating = "4.9", BackgroundColor = "#FE2E47", ImageUrl="teacher1.png", TeacherTitle="Designer" },
            new Subjects{Id = 2, Name = "Animation in After Effects", Teacher = "Tiana Mango", Rating = "4.3", BackgroundColor = "#FDB2C0", ImageUrl="teacher2.png", TeacherTitle="Animator" },
            new Subjects{Id = 3, Name = "Mobile App Design", Teacher = "Dulce Bator", Rating = "4.1", BackgroundColor = "#D0BCD0", ImageUrl="teacher3.png", TeacherTitle="Designer" },
            new Subjects {Id = 4, Name = "3D Design", Teacher = "Lincoln Bator", Rating = "4.6", BackgroundColor = "#88B5FC", ImageUrl="teacher4.png", TeacherTitle="3D Designer" }, 
            new Subjects{ Id = 5, Name = "Mobile App Development", Teacher = "Livia Lubin", Rating = "4.7", BackgroundColor = "#FDB2C0", ImageUrl="teacher5.png", TeacherTitle="Developer" } };
            GetSubjects();
        }

        private void GetSubjects()
        {
            CVSubjects.ItemsSource = subjects;
            CVGridSubjects.ItemsSource = subjects;
        }

        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        { 
           Navigation.PushModalAsync(new DetailsPage());
        }
    }

我怎么知道从我的 CollectionView 传递数据,我尝试通过参数传递数据,但出于某种原因,当我将名称添加到例如包含主题名称的标签时,我无法访问它在 c# 代码中,有人知道如何解决这个问题吗?

CollectionView 内置了处理选择的事件,没有必要使用 GestureRecognizer

<CollectionView SelectionMode="Single" 
                SelectionChanged="OnCollectionViewSelectionChanged" ... >

然后你可以通过它的构造函数将选中的项目传递到下一页

void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var current = (e.CurrentSelection.FirstOrDefault() as Subjects);
    Navigation.PushModalAsync(new DetailsPage(current));
}