简体   繁体   English

有什么方法可以在Listview上双击事件并将列表数据发送到另一个页面 Xamarin Forms

[英]Is there any way to have a double click event on Listview and send list data to another page in Xamarin Forms

This is what I have done now that I am unable to send ID to another page.这就是我现在所做的,因为我无法将ID发送到另一个页面。 And there's an error at Command Parameter as null reference exception.命令参数出现错误,为 null 引用异常。

Xaml: Xaml:

 <ListView x:Name="studentListView" ItemsSource="{Binding .} "> 
                    <ListView.ItemTemplate >
                        
                        <DataTemplate  >
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding StudentID}"></Label>
                                    <Label Text="{Binding FirstName}"></Label>
                                    <Label Text="{Binding LastName}"></Label>
                                    <Label Text="{Binding Gender}"></Label>
                                    <Label Text="{Binding Age}"></Label>
                                    <Label Text="{Binding Class}"></Label>
                                    <StackLayout.GestureRecognizers>
                                        <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding StudentID}"/>
                                        <TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped_1"/>
                                    </StackLayout.GestureRecognizers>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

C# Code below: C# 验证码如下:

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
       TapGestureRecognizer tapGesture = sender as TapGestureRecognizer;
            int clubid = (int) tapGesture.CommandParameter;
            Navigation.PushAsync(new StudentPage(clubid));
        }

You could also directly add a TapGestureRecognizer on the StudentID of the Label and then get the ID value to send it to another Page.也可以直接在LabelStudentID上添加一个TapGestureRecognizer ,然后获取ID值发送到另一个Page。 Here's the code below for your reference:以下是供您参考的代码:

Student Page Xaml:学生页面 Xaml:

    <ListView x:Name="studentListView" > 
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding StudentID}" WidthRequest="100" >
                                    <Label.GestureRecognizers  >
                                        <TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped">
                                        </TapGestureRecognizer>
                                    </Label.GestureRecognizers>
                                </Label>
                                <Label Text="{Binding FirstName}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Code in Back-end:后端代码:

     public StudentList() 
        {

            InitializeComponent();

            studentListView.ItemsSource = new List<MyStudent>
            {

                new MyStudent{StudentID="1", FirstName="Steve"},
                new MyStudent{StudentID="2", FirstName="Lorence"}
            };
        }

        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
           //get the StudentID and pass it through constructor
            var s = (sender as Label).Text;
            Navigation.PushModalAsync(new MainPage(s));
        }

Then, get the StudentID via Constructor in another page.然后,在另一个页面通过Constructor获取StudentID

  public MainPage(string a)
  {
       InitializeComponent();

       App.Current.MainPage.DisplayAlert("Student Id is", a, "OK");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms Xamarin.forms ListView单击到下一页 - Xamarin.forms ListView click to next page Xamarin Forms 上的 ListView 仅在您重新保存 xml 页面时才显示数据……知道吗? - ListView on Xamarin Forms only shows data when you resave the xml page… Any idea? 区分加载到列表项与滚动事件Xamarin表单ListView的项目 - Differentiate items loading to list vs scroll event Xamarin forms listview 如何在列表视图中通过单击项目打开另一个 xamarin 表单页面 - How to open another xamarin forms page form a item-click in a list view 在 xamarin.forms 中将标签从一页添加到另一页的列表视图 - Adding labels to a listview from one page to another in xamarin.forms 如何在xamarin表单中选择listview项目时打开另一个页面? - How to open another page when a listview item is selected in xamarin forms? 如何从另一个页面访问 ListView?(Xamarin Forms) - How to access a ListView from another page?(Xamarin Forms) MVVM具有不同页面xamarin形式的列表数据 - MVVM with list data on different page xamarin forms 有什么办法可以在导航栏上逐页在xamarin表单上添加背景图像 - Is there any way to add background Image on navigationbar page by page on xamarin forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM