Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - LongListSelector: Item tap?

I am using LongListSelector control on Windows Phone 8 and can't figure out the best way to handle a tap on an item. The few examples I've found rely on the SelectionChanged event. However, this solution is buggy because if I tap an item that opens a new page, hit back, and then tap the same item again, it won't work because this item is already selected, so SelectionChanged is not triggered.

I tried to register to the tap event and use the current selected item as the tapped one, but some times the current selected item is not the one I expect.

I could wrap my ItemTemplate in a button and handle the tap for each item but I need to reskin the button to make it look like a simple list item.

Finally, I don't understand why it is so complicated to achieve such a basic thing. Is there a simple and standard way I missed?

My second wish is to get an effect on the item when it is tapped. Is there any standard way to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could null your LongListSelector's SelectedItem at the end of each SelectionChanged event. I.e.

<phone:LongListSelector x:Name="LLS" SelectionChanged="LLS_SelectionChanged">

And the event handler:

private void LLS_SelectionChanged(object sender, SelectionChangedEventArgs e) {

  // If selected item is null, do nothing
  if (LLS.SelectedItem == null)
    return;

  // Navigate to the next page
  NavigationService.Navigate(new Uri("/nextpage.xaml", UriKind.Relative));

  // Reset selected item to null
  LLS.SelectedItem = null;
}

You'll fire the SelectionChanged event twice, but nothing's going to happen the second time round and you should get the behaviour that you're looking for - (i.e Setting SelectedItem to null will trigger a new SelectionChanged event, but this second event gets caught in the if-statement)

As for the second part of your question, you might be better posting a new question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...