0
votes

I'm using MVVM Light for my application and I have also implemented the INavigationService for going back/for between pages.

So in a common scenario, it's like this

MainPage > Categories > Rounds > DataPage.

In the DataPage, I'm making a request to fetch the results and depending on the result returned from the callback I call the .GoBack() method to pop the current page from the stack and return to Rounds.

What I have noticed is that if I hit first the DataPage and the .GoBack() gets called and then tap on a different round the callback method will be fired twice, and if I go back and in again thrice, and continues like this.

Essentially this means that the .GoBack() will be called again and the navigation gets messed up.

I believe this has to do with not cleaning up the previous VM's, I tried changing this behavior with the UnRegister / Register class from SimpleIOC but no luck.

1
Sounds like you're hooking up the event when you enter but not unhooking it when you leave. I'd remove the callback from the event inside the callback.Slepz
@Slepz I was trying to do that and did it another way now, check my proposed answer and tell me your comment.Dimitris Batsougiannis

1 Answers

0
votes

In the ViewModel class

public void UnsubscribeFromCallBack()
{
  this.event -= method;
}

In the .xaml.cs page

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        PageViewModel vm = (this.BindingContext as PageViewModel);
        vm.UnSubscribeFromCallback();
    }