0
votes

I'm developing a WP8.1 application who uses MVVM Light. Basically it works fine. Except a strange behavior with the navigation. I have a command in a VM that is bound to the selectionChanged Event of a list:

        private void GoToTransactionList()
        {
            if (SelectedAccount != null)
            {
                ((Frame)Window.Current.Content).Navigate(typeof(TransactionList));
            }
        }

The View doesn't have any code behind and the whole content is on a user control. This is the VM:

public class TransactionListUserControlViewModel : ViewModelBase
{
    private Account SelectedAccount
    {
        get { return ServiceLocator.Current.GetInstance<AccountDataAccess>().SelectedAccount; }
    }

    public ObservableCollection<FinancialTransaction> RelatedTransactions
    {
        get { return ServiceLocator.Current.GetInstance<TransactionDataAccess>().RelatedTransactions; }
    }

    public RelayCommand LoadRelatedTransactionsCommand { get; private set; }

    public TransactionListUserControlViewModel()
    {
        LoadRelatedTransactionsCommand = new RelayCommand(LoadRelatedTransactions);
    }

    private void LoadRelatedTransactions()
    {
        ServiceLocator.Current.GetInstance<TransactionDataAccess>().GetRelatedTransactions(SelectedAccount.Id);
    }

    public void Dispose()
    {
        this.Cleanup();
    }
}

My Locator looks like this:

public TransactionListUserControlViewModel TransactionListControl
{
   get { return new TransactionListUserControlViewModel(); }
}

Or:

public TransactionListUserControlViewModel TransactionListControl
{
    get { return ServiceLocator.Current.GetInstance<TransactionListUserControlViewModel>(); }
}

I tried both. But don't change the behavior.

Now, I navigate to the List for the first time I have to click the back button once to navigate back. If i navigate again to the page, I have to click twice and so on. In other words the View won't be disposed but for each time I navigate to the view a new obect is generated. I assume that depends on something who is missing on the VM.

For a better overview I add here the link to the github Repo.

Can anyone tell me what I have forgotten?

Thanks for your help!

1
can't you make the back button event trigger the dispose?sexta13
The problem is that I don't know exactly what I have to dispose.I assume there is A) a dependency I don't see and prevents the object from getting disposed or B) that I miss a default command..NPadrutt
If you have a UserControl, my advice to you is that in the viewmodellocator you create a new instance everytime. something like: public MyUserControlViewModel XPTO {get{return new MyUserControlView();}}sexta13
I added some code of my locator. Isn't it what you meant?NPadrutt

1 Answers

1
votes

I found the bug. It was caused by a binding in my UserControl. So when you navigate back, it set the value again and navigated again till the value was set to null (what always was after x times and x was the number of navigations to the page ^^).

Fixed it with a seperate variable for the selectedItem and used it in the navigationCommand:

    private void GoToTransactionList()
    {
        if (SelectedItem != null)
        {
            SelectedAccount = SelectedItem;
            ((Frame)Window.Current.Content).Navigate(typeof(TransactionList));
            SelectedItem = null;
        }
    }