3

在详细信息页面上,我有一个列表视图和一个控件,该控件根据列表视图中的选定项目显示详细信息。在主页上,显示了该集合的几个项目(再次显示在详细信息页面中)。如果用户单击其中一个,它将导航到详细信息页面并显示详细信息。不幸的是,每次用户单击主页上的某个项目时,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

}

谢谢您的帮助!

4

3 回答 3

1

从 XAML 代码中删除 SelectionChanged 处理程序。在您的代码隐藏中,将 SelectedIndex 设置为 -1(或默认项的索引,如果有),然后在设置 SelectedIndex 后添加处理程序。

编辑:只是为了提供一些背景,如果列表视图的选定项以编程方式更改(例如,当您为 SelectedIndex 或 SelectedItem 属性分配新值时),也会触发 SelectionChanged 事件。我不太确定当您更改 ItemsSource 时它是否会触发,但我认为会是这种情况。因此,您希望在完成初始化后添加事件处理程序。

于 2012-10-06T13:52:37.953 回答
1

我尝试了同样的问题。我发现“SelectionChanged”事件被注册了两次。一次在页面构造器上,另一次在 *.g.cs 文件上,这看起来是一个“设计器”文件。

我解决了它删除类的构造函数上的选择更改注册。

我希望它可以帮助你。

于 2014-12-29T13:58:17.633 回答
-1

我认为您需要决定在 SelectionChanged 事件中响应什么(也许您想取消选择已选择的内容?我不确定您要做什么)。也许这篇文章可能会有所帮助。

于 2012-10-03T20:49:15.937 回答