Xamarin 等待 PushAsync 行为
Xamarin await PushAsync behavior
假设我有这个代码:
try
{
await ((MasterDetailPage)Application.Current.MainPage).
Detail.Navigation.PushAsync(new SecondPage());
}
finally
{
Console.WriteLine("Completed");
}
输出"Completed"时,SecondPage是只push到导航栈还是已经push了内容也加载了?换句话说,"await PushAsync" 等待整个页面加载或仅等待推送?
你可以做一个简单的测试来找出顺序:
主页中:
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
await Navigation.PushAsync(new Page1());
}
finally
{
Console.WriteLine("Completed");
}
}
在第 1 页中:
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
Console.WriteLine("Page1" + "Construction");
}
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Page1" + "OnAppearing");
}
}
结果是:
因此,当输出Completed
时,该页面在您的应用程序中为visible
,您可以在OnAppearing
或Construction
方法中加载数据。
假设我有这个代码:
try
{
await ((MasterDetailPage)Application.Current.MainPage).
Detail.Navigation.PushAsync(new SecondPage());
}
finally
{
Console.WriteLine("Completed");
}
输出"Completed"时,SecondPage是只push到导航栈还是已经push了内容也加载了?换句话说,"await PushAsync" 等待整个页面加载或仅等待推送?
你可以做一个简单的测试来找出顺序:
主页中:
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
await Navigation.PushAsync(new Page1());
}
finally
{
Console.WriteLine("Completed");
}
}
在第 1 页中:
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
Console.WriteLine("Page1" + "Construction");
}
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Page1" + "OnAppearing");
}
}
结果是:
因此,当输出Completed
时,该页面在您的应用程序中为visible
,您可以在OnAppearing
或Construction
方法中加载数据。