简体   繁体   English

带有导航的CallMethodAction导致多个导航调用

[英]CallMethodAction with navigation causes multiple navigate calls

I want to navigate to a new page when item is clicked in a ListView . ListView单击项目时,我想导航到新页面。 I have following XAML (using Behaviors). 我有以下XAML(使用行为)。

<ListView Grid.Row="1" ItemsSource="{Binding Subreddits}" Margin="-10,20,0,0" SelectionMode="None" IsItemClickEnabled="True">   
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="ItemClick">
            <Core:CallMethodAction TargetObject="{Binding Mode=OneWay}" MethodName="SubredditTapped"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</ListView>

I have enabled caching on my page as follows: 我已在页面上启用缓存,如下所示:

public MyPage1()
{
    this.InitializeComponent();
    NavigationCacheMode = NavigationCacheMode.Required;     
}

Here's my SubredditTapped method: 这是我的SubredditTapped方法:

public void SubredditTapped(object sender, object parameter)
{
    var args = parameter as Windows.UI.Xaml.Controls.ItemClickEventArgs;
    var clickedItem = args.ClickedItem as Subreddit;
    string navigationParamName = "Subreddit";
    BootStrapper.Current.SessionState[navigationParamName] = clickedItem;
    BootStrapper.Current.NavigationService.Navigate(typeof(MyPage2), navigationParamName);
    return;           
}

Now, when I tap an item for the first time, frame navigates to MyPage2 as expected. 现在,当我第一次点击一个项目时,框架MyPage2预期导航到MyPage2 When I navigate back to MyPage1 (using back button) and then again navigate to MyPage2 (by tapping an item) the SubredditTapped method is called twice. 当我导航回MyPage1 (使用“后退”按钮),然后再次导航至MyPage2 (通过点击一个项目)时, SubredditTapped方法被调用两次。 This number goes on increasing as I navigate back and forth. 随着我来回导航,这个数字还在不断增加。
Since disabling cache on MyPage1 gets rid of this problem, I suspect that the method SubredditTapped is added to delegate list of ItemClick every time MyPage1 is navigated to. 由于上禁用缓存MyPage1摆脱这个问题,我怀疑方法SubredditTapped被添加到委托列表ItemClick每次MyPage1导航到。 How do I prevent this from happening? 如何防止这种情况发生? What is the correct way to do this without breaking MVVM? 不破坏MVVM的正确方法是什么?

Edit: The bug in Behaviours SDK has been fixed in newer versions. 编辑: Behaviors SDK中的错误已在较新版本中修复。 So this works. 所以这可行。

On the ViewModel the has the Subreddits, create a property of the Subreddit type for the SelectedSubreddit, and bind SelectedItem of the ListBox to this new property with a 2way binding. 在ViewModel上具有Subreddit,为SelectedSubreddit创建Subreddit类型的属性,然后使用2way绑定将ListBox的SelectedItem绑定到此新属性。 Then in the setter of the new property ( which will get called when a list item is selected ) you can call the method there. 然后,在新属性的设置器中(选择列表项时将调用该属性),您可以在那里调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM