App.xaml.cs
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
}