-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
65 lines (52 loc) · 2.08 KB
/
App.xaml.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace DevWinUIGallery;
public partial class App : Application
{
public static Window MainWindow = Window.Current;
public IServiceProvider Services { get; }
public new static App Current => (App)Application.Current;
public IJsonNavigationService GetJsonNavigationService => GetService<IJsonNavigationService>();
public IThemeService GetThemeService => GetService<IThemeService>();
public static T GetService<T>() where T : class
{
if ((App.Current as App)!.Services.GetService(typeof(T)) is not T service)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
}
return service;
}
public App()
{
Services = ConfigureServices();
Application.Current.RequestedTheme = GetThemeService.GetApplicationTheme();
this.InitializeComponent();
}
private static IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddSingleton<IThemeService, ThemeService>();
services.AddSingleton<IJsonNavigationService, JsonNavigationService>();
services.AddTransient<MainViewModel>();
services.AddTransient<GeneralSettingViewModel>();
services.AddTransient<AppUpdateSettingViewModel>();
services.AddTransient<AboutUsSettingViewModel>();
return services.BuildServiceProvider();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
MainWindow = new Window();
if (MainWindow.Content is not Frame rootFrame)
{
MainWindow.Content = rootFrame = new Frame();
}
if (GetThemeService != null)
{
GetThemeService.AutoInitialize(MainWindow)
.EnableRequestedTheme()
.ConfigureTintColor();
}
rootFrame.Navigate(typeof(MainPage));
MainWindow.Title = MainWindow.AppWindow.Title = ProcessInfoHelper.ProductNameAndVersion;
MainWindow.AppWindow.SetIcon("Assets/icon.ico");
MainWindow.Activate();
}
}