在详细信息页面上,我有一个列表视图和一个控件,该控件根据列表视图中的选定项目显示详细信息。在主页上,显示了该集合的几个项目(再次显示在详细信息页面中)。如果用户单击其中一个,它将导航到详细信息页面并显示详细信息。不幸的是,每次用户单击主页上的某个项目时,selection-changed 事件都会引发两次:第一个项目(默认项目)一次,所选项目一次。我怎样才能正确处理这个?- 我只需要第二个(真实的)事件。
我找到了这个帖子,但无法解决我的问题......
这是我的代码(缩短):
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
}
谢谢您的帮助!