使用 Xamarin.Forms 默认的模板生成卡片式 App 项目后,App 打开的是第一个卡片页。实用中,往往需要 App 启动后呈现一个 Splash,在 Splash 页面的后台完成系统初始化的一些任务,然后自动跳转或者等待用户点击 “立即体验”/“开始使用” 等按钮,App 再呈现具体的内容。
那么如何正确,安全地实现页面跳转呢?
8
White
White
Silver
DarkBlue
#505060
White
#1976D2
White
#1976D2
White
#1976D2
#A3FFFFFF
#F0F0F0
#3060C0
Red
Silver
App.xaml 中没有什么功能性的内容,它只是一个启动器,在 App.xaml 中定义了一堆颜色和样式。当然还可以在这里定义更多的资源,以
using System;
using System.IO;
using System.Net.Http;
using I2oT.Data;
using I2oT.Models;
using I2oT.Views;
using Xamarin.Forms;
namespace I2oT
{
public partial class App : Application
{
static I2oTDatabase database;
static RemoteGateway gateway;
// Create the database connection as a singleton.
public static I2oTDatabase Database
{
get
{
if (database == null)
{
database = new I2oTDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "I2oT.db"));
}
return database;
}
}
public static RemoteGateway Gateway
{
get
{
if(gateway == null)
{
gateway = new RemoteGateway();
}
return gateway;
}
}
public App()
{
InitializeComponent();
MainPage = new SplashPage();
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
App.xaml.cs 是功能性代码。我的这个例子中,定义了两个静态对象,分别是
MainPage = new SplashPage();
就是它!
MainPage 是 Application 的一个属性,指定 MainPage 相当于把 Application 的当前控制权交给新的对象。在这里,SplashPage 是已经定义好的引导页。