Reputation: 13786
I'm using Navigation.PushAsync
to navigate to a new page and when the work is done, it will call PopAsync
to return to previous page. The problem is the first page needs to wait to the new page to pop and then continue some work. So I'm doing like this:
// source page:
DoWork();
await Navigation.PushAsync(new TargetPage()); <== this does not wait for the page!??
DoOtherWork();
I observed that the DoOtherWork
part runs immediately when the new page is shown, not until it is dismissed. But I'm using await
here, so that means the later part should be a continuation upon the async task is finished, right? Why is it run without waiting for the Navigation task?
Upvotes: 0
Views: 1012
Reputation: 7552
You can do that in this way:
public async Task<bool> openContentPageForResult()
{
TaskCompletionSource<bool> waitToCloseTask = new TaskCompletionSource<bool>();
MyPage myNewPage = new MyPage();
myNewPage.Disappearing += (sender2, e2) =>
{
waitToCloseTask.SetResult(true);
};
await App.Current.MainPage.Navigation.PushAsync(myNewPage);
return await waitToCloseTask.Task;
}
Then just call this method and await for the result, in this case a boolean result.
bool myResult = await openContentPageForResult();
Upvotes: 0
Reputation: 5768
await Navigation.PushAsync(new TargetPage());
This code "await" until the "TargetPage" is visualized, then code continue with "OnOtherCode".
If you want to execute some code after TargetPage is closed, you should add this code after Navigation.PopAsync
, or in "TargetPage's OnDisappearing" event.
Another solution can be: use a MessagingCenter.Send
in "TargetPage's OnDisappearing" event and subscribe to the same message in the Calling Page
Upvotes: 3