On a detail page, I have a listview and a control that shows the details according to the selected item in the listview. On the main page, there are several items shown of this collection (which are again shown in the detail page). If the user clicks one of them, it navigates to the detail page and shows the details. Unfortunately, every time a user clicks an item on the main page, the selection-changed event gets raised two times: Once for the first item (default item) and one for the selected item. How can I correctly handle this? - I only need the second (real) event.
I found this post, but couldn't solve my problem...
This is my code (shortened):
MainPage.xaml:
<GridView
x:Name="itemGridView"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource Custom250x250ItemTemplate}"
SelectionMode="None"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick">
<!-- details -->
</GridView>
MainPage.xaml.cs:
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
// Navigate to the appropriate destination page, configuring the new page
// by passing required information as a navigation parameter
var itemId = ((ArticleDataItem)e.ClickedItem).Id;
this.Frame.Navigate(typeof(ItemDetailPage), itemId);
}
ItemDetailPage.xaml:
<ListView
x:Name="itemListView"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
IsSwipeEnabled="False"
SelectionChanged="ItemListView_SelectionChanged"
ItemTemplate="{StaticResource Standard130ItemTemplate}"
ItemContainerStyle="{StaticResource CustomListViewItemStyle}"
/>
<!-- ... -->
<WebView x:Name="contentBrowser" />
ItemDetailPage.xaml.cs:
void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// this event gets raised two times when navigating from the MainPage to this page !!!
// Invalidate the view state when logical page navigation is in effect, as a change
// in selection may cause a corresponding change in the current logical page. When
// an item is selected this has the effect of changing from displaying the item list
// to showing the selected item's details. When the selection is cleared this has the
// opposite effect.
if (this.UsingLogicalPageNavigation()) this.InvalidateVisualState();
if (itemsViewSource.View.CurrentItem == null)
{
return;
}
// reset contentBrowser-Content
contentBrowser.NavigateToString(Tasks.WebViewPreCode + "<p>Loading...</p>" + Tasks.WebViewAfterCode);
var selectedItem = (ArticleDataItem)this.itemsViewSource.View.CurrentItem;
if ( selectedItem == null )
{
contentBrowser.NavigateToString(Tasks.WebViewPreCode + "<p>There was an error. Please try again later.</p>" + Tasks.WebViewAfterCode);
return;
}
// Download item details
ArticleDataSource.ReadArticle(selectedItem); // this really shouldn't be called two times
}
thank you for your help!