How to clear old ContentPageX when calling new Navigation.PushAsync(new ContentPageX())

UdayBhaskarPonguluruUdayBhaskarPonguluru USMember
edited January 2017 in Xamarin.Forms

I am navigating to a ContentPageX on ListView OnItemSelected event using Navigation.PushAsync(new ContentPageX(itemSelected))
Now I Navigated Back to ListView page and then selected a different list view item which navigated to ContentPageX with different item selected.

I am facing issue as the old viewmodel still exists in memory and the property changed event raising multiple times for each of the old ContentPageX.

Now how to clear the old content page which has view model associated. Or is my navigation approach wrong?
I want to create a new content page and remove old content page when i navigate using new ContentPageX().
Removing it from NavigationStack on DisAppearing doesn't help as it is getting removed from the NavigationStack but the actual ContentPageX object still exists.

ListViewPage:

    public void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null) return;

        Navigation.PushAsync(new ContentPageX(e.SelectedItem));

        ((ListView)sender).SelectedItem = null;
    }

ContentPageX:

public partial class ContentPageX : ContentPage
{
public ContentPageX (Item itemSelected)
{
InitializeComponent();

        BindingContext = new ContentPageXViewModel(itemSelected);
    }
}

ViewModel:

public class ContentPageXViewModel : INotifyPropertyChanged
{
    public ContentPageXViewModel(ItemitemSelected)
    {
        DependencyService.Get<IClipboardService>().FireClipboardValueChangeNotifyEvent(this);
    }

}

Tagged:

Answers

Sign In or Register to comment.