App.xaml.cs 1.3 KB
namespace IndustrialControl;

public partial class App : Application
{
    private readonly IConfigLoader _configLoader;
    public static IServiceProvider? Services { get; set; }
    public App(IServiceProvider sp, IConfigLoader configLoader)
    {
        InitializeComponent();

        _configLoader = configLoader;
        // 1) 立刻给 MainPage 一个默认值,避免 null
        MainPage = new AppShell(authed: false); // 显示:登录|日志|管理员
        // 启动根据是否已登录选择壳
        _ = InitAsync();

    }

    protected override async void OnStart()
    {
        base.OnStart();

        // 1) 启动时确保配置已覆盖(这里才能 await)
        await _configLoader.EnsureLatestAsync();

        // 2) 判断是否已登录
        var token = await TokenStorage.LoadAsync();
        var isLoggedIn = !string.IsNullOrWhiteSpace(token);
    }

    private async Task InitAsync()
    {
        var token = await TokenStorage.LoadAsync();
        if (!string.IsNullOrWhiteSpace(token))
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                Current.MainPage = new AppShell(authed: true); // 显示:主页|日志|管理员
            });
        }
    }

    public static void SwitchToLoggedInShell() => Current.MainPage = new AppShell(true);
    public static void SwitchToLoggedOutShell() => Current.MainPage = new AppShell(false);
}