执行异步任务,我想执行下一次使用Xamarin:上启动
当应用程序启动时启动画面立即呈现(我已经达到了这个以下一个教程https://channel9.msdn.com/Blogs/MVP-Windows-Dev/Using-Splash-Screen-with-Xamarin-Forms)
执行数据库迁移如果有(用户更新的应用程序,并在第一次运行)
从数据库中读取用户数据(用户名和密码),调用REST webservice来检查用户数据是否仍然有效。如果用户的数据是有效的将用户重定向到炫魅否则重定向到LoginPage
我读过关于未来良好Xamarin.Forms Async Task On Startup后。当前代码:
public class MainActivity :global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new App()); // method is new in 1.3
}
}
// shared code - PCL lib for Android and iOS
public partial class App : Application
{
public App()
{
InitializeComponent();
// MainPage = new LoadingPage();
}
}
protected override async void OnStart()
{
// Handle when your app starts
await App.Database.Migrations();
if(await CheckUser()) // reads user data from db and makes http request
this.MainPage = new Layout.BrowsePage();
else
this.MainPage = new LoginPage();
}
如果MainPage
在构造异常未设置被套上iOS和Android。我知道异步void不会等待,如果它不明确地有.Wait()
- Async void,但这是否意味着执行线程仍然继续它的工作。
执行线程命中时await App.Database.Migrations();
它暂停执行并等待等待Task完成。同时它继续它的工作(即LoadApplication()继续执行,并期望现在设置App.MainPage)。我的假设是否正确?对于异步/等待我来说,我很新。
我只是想避免LoadingPage
因为三分屏显示:
- 闪屏(权当应用程序被启动)
- LoadingPage(DB迁移,HTTP请求,..)
- BrowsePage或LoginPage
对于用户体验而言,理想的只是两页。
我弄成这个样子,但我相信有一个更好的方法:
protected override void OnStart()
{
Page startPage = null;
Task.Run(async() =>
{
await App.Database.Migrations();
startPage = await CheckUser() ? new Layout.BrowsePage() : new LoginPage();
}.Wait();
this.MainPage = startPage();
}
你在'更好的方法'中寻找什么?为什么你认为显示一个加载页面是一个劣质的用户体验? –
我想,如果应用程序需要5-8秒加载然后加载页面就可以了。它向用户显示正在发生的事情(正在进行迁移......)。我测量了时间,启动应用程序需要7秒(Samsung Galaxy A5)。 – broadband