How to refresh the one but last ContentPage on the Navigation

前端 未结 3 1084
北海茫月
北海茫月 2020-12-19 13:37

Typically, one pops the current page using this from the NavigationStack:

   Navigation.PopAsync( true );

How to I use Naviga

3条回答
  •  醉话见心
    2020-12-19 14:31

    As @SushiHangover mentioned, MessagingCenter is a good option.

    Another way would be to subscribe to Page2's OnDisappearing() event from page1 and do something to the Page1 UI/data like so:

    Edit: The old way I answered this question (see changelog) does work but I have since modified how I do it, after seeing others' examples, to prevent memory leaks. It is better to unsubscribe from the Disappearing event after it has been used. If you plan to use it again then you can just resubscribe to it before running PushAsync() again on your Page2 instance:

    private async void OnGoToPage2Clicked(object sender, EventArgs args) {
        Page2 page2 = new Page2();
    
        page2.Disappearing += OnPage2Disappearing;
    
        await Navigation.PushAsync(page2);
    }
    
    private async void OnPage2Disappearing(object sender, EventArgs eventArgs) {
        await _viewModel.RefreshPage1Data(); //Or how ever you need to refresh the data
        ((Page2)sender).Disappearing -= OnPage2Disappearing; //Unsubscribe from the event to allow the GC to collect the page and prevent memory leaks
    }
    

提交回复
热议问题