2014-12-05 7 views
7

sto effettivamente lavorando con questo pezzo di codiceXamarin - compensazione selezione ListView

using System; 
using Xamarin.Forms; 
using System.Diagnostics; 

namespace CryptoUI 
{ 
    public class HomePage : Xamarin.Forms.MasterDetailPage 
    { 
     public HomePage() 
     { 
     // Set up the Master, i.e. 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, i.e 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); 
     } 
    } 
} 

Questo codice mostra in realtà un menu a comparsa semplice, utilizzando il Xamarin Forms tecnologie. Attualmente sto cercando di capire come ho potuto cancellare facilmente la selezione di ListView dopo aver selezionato la pagina a cui voglio andare!

Ho trovato questo pezzo di codice sul sito Web di Xamarin per gli sviluppatori (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 
}; 

Ma io non riesco a capire come attualmente dovrei integrarlo con il mio codice sopra ci :)

+1

Per prima cosa non è necessario separare gli eventi ItemSelected per farlo. Penso che puoi semplicemente chiamare 'listview.SelectedItem = null' per ripristinare la proprietà listview selecteditem. Prima o dopo aver premuto la nuova pagina. Secondo, potrebbe essere meglio usare 'ItemTapped' per iniziare la transizione della pagina e usare itemselected per elaborare/passare i dati alla vista successiva. –

risposta

11

Stai assegnando il gestore itemSelected due volte, che è una cattiva idea. Tutto si dovrebbe avere a che fare è aggiungere questa linea al gestore itemSelected esistente

((ListView)sender).SelectedItem = null; 
+0

Grazie per la pronta risposta! Comunque ho già provato prima, tutto quello che sto ottenendo è questo: http://1drv.ms/1s0K0cO non appena clicco su qualsiasi elemento in ListView –

+0

Se aggiungi questa riga all'inizio del tuo gestore ItemSelected, allora tutto i successivi riferimenti a SelectedItem si interromperanno perché ora è nullo. Prova ad aggiungerlo nella parte inferiore del gestore, dopo che tutto l'altro codice di riferimento è stato fatto. – Jason

+0

Beh, se guardi lo screenshot che ho collegato lì, sto facendo quello che stai dicendo, ancora tutto sta andando in crash :( –

8

vorrei aggiungere alla risposta di Jason perché manca alcune informazioni vitali. Quando si imposta la proprietà ListView SelectedItem su null, verrà generato nuovamente l'evento ItemSelected. Quindi, se non si dispone di un controllo Null, verrà generata un'eccezione.

Questo è quello che dovrebbe essere simile:

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

Ho avuto questo stesso problema, ma le altre soluzioni non ha funzionato per me. Poiché avevo bisogno di passare un oggetto personalizzato alla pagina successiva, ho annullato il riferimento all'elemento selezionato e ho utilizzato il riferimento toccato dell'oggetto per il mio oggetto personalizzato.

listView.ItemTapped += async (sender, e) =>{ 

    await Navigation.PushAsync(new DetailPage(e.Item as CustomObject)); 
    ((ListView)sender).SelectedItem = null; 

};