如何在ListView中禁用默认选择?

3
在详细页面中,我有一个列表视图和一个控件,根据列表视图中选择的项目显示详细信息。在主页面上,显示了该集合的几个项目(这些项目再次显示在详细页面中)。如果用户单击其中一个项目,它会导航到详细页面并显示详细信息。不幸的是,每当用户在主页面上单击一个项目时,选择更改事件会被触发两次:一次是默认项目的第一个项目,一次是所选项目。如何正确处理? - 我只需要第二个(真实)事件。
我找到了post,但无法解决我的问题...
这是我的代码(缩短版):

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

}

谢谢您的帮助!


你能展示一下你目前正在使用的代码,这个问题是在哪里发生的吗? - MethodMan
3个回答

1
我遇到了同样的问题。 我发现“SelectionChanged”事件被注册了两次。 一次在页面构造函数中,另一次在*.g.cs文件中,看起来像是一个“设计师”文件。
我通过在类的构造函数中删除选择更改注册来解决它。
希望这能对你有所帮助。

1

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

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


你好Akinwale, 感谢您的回答。不幸的是,那并没有帮助我。正如您所描述的,我从XAML代码中删除了SelectionChanged处理程序,并在设置selectedIndex为-1后,在代码后台(构造函数)中添加了它。我可能做错了什么,或者还有其他解决问题的方法吗?-谢谢! - casaout
不幸的是,我无法解决我的问题,但必须选择一个答案。这是可能答案中最好的建议... - casaout

-1

我认为你需要决定在SelectionChanged事件中如何响应(也许你想取消选择某些已选内容?我不确定你的意图)。或许这篇帖子可以帮到你。


我试图改进我的问题描述...感谢您的帮助! - casaout
好的。我仍然有些困惑,但是你有考虑过你的应用程序设计吗?为什么选择不使用MVVM和Command接口?通过避免响应事件并更好地设计您的应用程序,可能可以解决您的问题。 - sfogle
我正在使用MVVM - 我认为(并且我在第一篇帖子中添加的链接支持这一点),这与ListView有关... - casaout
如果您正在使用MVVM,我会尝试将ListView的SelectedItem绑定到ViewModel中的属性。根据此属性设置的内容,ViewModel将为详细信息页面提供适当的详细信息页面XAML进行绑定。您也可以在ViewModel中处理导航,但仅使用INotifyPropertyChanged接口可能就足够了。在我看来,您要么没有使用MVVM,要么没有充分利用它。 - sfogle
正如您所见,我有一个WebView控件来显示详细信息。不幸的是,您无法对此控件进行数据绑定。是否有人有任何想法,如何避免获取两个事件?-谢谢。 - casaout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接