繁体   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

这就是我现在所做的,因为我无法将ID发送到另一个页面。 命令参数出现错误,为 null 引用异常。

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# 验证码如下:

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

也可以直接在LabelStudentID上添加一个TapGestureRecognizer ,然后获取ID值发送到另一个Page。 以下是供您参考的代码:

学生页面 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>

后端代码:

     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));
        }

然后,在另一个页面通过Constructor获取StudentID

  public MainPage(string a)
  {
       InitializeComponent();

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM