我想要一个显示欢迎消息然后从网络服务加载数据的 Xamarin PCL ContentPage。我的 Web 服务正在运行,但问题是在下面的示例中,它需要一些时间才能运行,但没有显示任何内容。
一般策略是什么:显示等待类型的启动页面,等待长时间运行的任务完成,然后继续应用程序的其余部分(比如主菜单页面)?
希望该策略能在 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
}
}
最佳答案
这可能会成功:
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。
关于包含 Web 服务调用的 Xamarin PCL 初始屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100534/