如何在代码中声明listview的ItemSelected? C#

时间:2017-06-29 02:18:27

标签: c# listview xamarin.forms

我有一个以我的xamarin形式的listview,并且有ItemSelected和ItemTapped,以后可以在我的私有空间中使用。我想知道如何在代码中声明ItemSelected和ItemTapped。

<ListView ItemsSource="{x:Static local:interactiveListViewXaml.items}" ItemSelected="OnSelection" ItemTapped="OnTap" IsPullToRefreshEnabled="true" Refreshing="OnRefresh">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.ContextActions>


                    </ViewCell.ContextActions>
                    <StackLayout Padding="15,0">

                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我想创建listview并在代码中声明ItemSelected。下面有ItemSelected和ItemTapped的东西。

ListView mylist = new ListView
        {

            ItemsSource = test,


            ItemTemplate = new DataTemplate(() =>
            {

                Label nameLabel = new Label();
                nameLabel.SetBinding(Label.TextProperty, "Name");

                Label nameLabel2 = new Label();
                nameLabel2.SetBinding(Label.TextProperty, "reading");



                // Return an assembled ViewCell.
                return new ViewCell 
                {
                    View = new StackLayout
                    {
                        Padding = new Thickness(0, 5),
                        Orientation = StackOrientation.Horizontal,
                        Children =
                            {

                                new StackLayout
                                {
                                    VerticalOptions = LayoutOptions.Center,
                                    Spacing = 0,
                                    Children =
                                    {
                                        nameLabel,
                                        nameLabel2

                                    }
                                    }
                            }

                    }

                };


            })
        };

1 个答案:

答案 0 :(得分:0)

mylist.ItemTapped += (sender, e) => {
  // handler code goes here
};

mylist.ItemSelected += (object sender, SelectedItemChangedEventArgs e) => {
  // handler code goes here
};