Xamarin - ListView List Selection

I really work with this piece of code

using System; using Xamarin.Forms; using System.Diagnostics; namespace CryptoUI { public class HomePage : Xamarin.Forms.MasterDetailPage { public HomePage() { // Set up the Master, ie the Menu Label header = new Label { Text = "MENU", Font = Font.SystemFontOfSize(20, FontAttributes.Bold), HorizontalOptions = LayoutOptions.Center }; // create an array of the Page names string[] myPageNames = { "Main", "Page 2", "Page 3", }; // Create ListView for the Master page. ListView listView = new ListView { ItemsSource = myPageNames, }; // The Master page is actually the Menu page for us this.Master = new ContentPage { Title = "Test", Content = new StackLayout { Children = { header, listView }, } }; // Define a selected handler for the ListView contained in the Master (ie Menu) Page. listView.ItemSelected += (sender, args) => { // Set the BindingContext of the detail page. this.Detail.BindingContext = args.SelectedItem; string currentPage = this.GetType().Name.ToString(); // This is where you would put your "go to one of the selected pages" if(listView.SelectedItem.Equals("Main") && !currentPage.Equals("HomePage")){ AsyncPush(new HomePage()); } else if(listView.SelectedItem.Equals("Page 2") && !currentPage.Equals("SecondPage")){ AsyncPush(new SecondPage()); } else if(listView.SelectedItem.Equals("Page 3") && !currentPage.Equals("ThirdPage")){ AsyncPush(new ThirdPage()); } // Show the detail page. this.IsPresented = false; }; listView.ItemSelected += (senders, e) => { if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row // do something with e.SelectedItem ((ListView)senders).SelectedItem = null; // de-select the row }; // Set up the Detail, ie the Home or Main page. Label myHomeHeader = new Label { Text = "Home Page", HorizontalOptions = LayoutOptions.Center }; string[] homePageItems = { "Alpha", "Beta", "Gamma" }; ListView myHomeView = new ListView { ItemsSource = homePageItems, }; var myHomePage = new ContentPage(); myHomePage.Content = new StackLayout { Children = { myHomeHeader, myHomeView } , }; this.Detail = myHomePage; } public async void AsyncPush(Page page) { await Navigation.PushAsync(page); } } } 

This code actually shows FlyOut's lightweight menu using Xamarin Forms technology. I'm currently trying to figure out how I could easily clear my ListView selection after I chose which page I want to go to!

I found this piece of code on the Xamarin website for developers ( http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/ );

 listView.ItemSelected += (sender, e) => { if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row // do something with e.SelectedItem ((ListView)sender).SelectedItem = null; // de-select the row }; 

But I can’t understand now how can I integrate it with my code above :)

+7
c # listview xamarin xamarin.forms xamarin-studio
source share
3 answers

You assign an ItemSelected handler twice, which is a bad idea. All you have to do is add this line to the existing ItemSelected handler

  ((ListView)sender).SelectedItem = null; 
+11
source share

I would like to add an answer to Jason because he misses important information. When you set the ListView SelectedItem property to null, it closes the ItemSelected event again. Therefore, if you do not have a null check, it throws an exception.

Here's how it should look:

 void ItemSelected(object sender, EventArgs args) { if (((ListView)sender).SelectedItem == null) return; //Do stuff here with the SelectedItem ... ((ListView)sender).SelectedItem = null; } 
+8
source share

I had the same problem, but other solutions did not work for me. Since I needed to pass the custom object to the next page, I canceled the link to the selected item and used the item link for my custom object.

 listView.ItemTapped += async (sender, e) =>{ await Navigation.PushAsync(new DetailPage(e.Item as CustomObject)); ((ListView)sender).SelectedItem = null; }; 
+2
source share

All Articles