我想要一个显示欢迎消息的Xamarin PCL ContentPage,然后从Web服务加载数据。我有Web服务,但问题是在下面的示例中,在工作需要时间时不会显示任何内容。
一般策略是什么:显示等待类型的Splash页面,等待长时间运行的任务完成,然后继续应用程序的其余部分(比如主菜单页面)?
希望该策略能够在PCL项目中运行,这样我就不必尝试编写iOS和Android版本了吗?我试图将代码放在各种覆盖方法中,但似乎没有任何工作。提前感谢任何建议。
// The following is called from the "App : Application" class.
public class SplashScreen : ContentPage
{
public SplashScreen()
{
Label lblWelcome = new Label { Text = "Hello. Please wait..." };
Content = new StackLayout
{
Children = { lblWelcome }
}
//TODO: Have the device display the above content before the following continues...
CallWebServiceToLoadParameters();
//TODO: Move on to display the Main Menu
}
}
答案 0 :(得分:3)
这可以解决问题:
public class SplashScreen : ContentPage
{
public SplashScreen()
{
Label lblWelcome = new Label { Text = "Hello. Please wait..." };
Content = new StackLayout
{
Children = { lblWelcome }
}
Device.BeginInvokeOnMainThread(async () => {
//If awaitable
var response = await CallWebServiceToLoadParameters();
if(response{
App.Current.MainPage = //Your main page.
}
});
}
}
有点难看,正确的方法是使用每个页面的ViewModel。