This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathApp.xaml.cs
70 lines (56 loc) · 2.19 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
66
67
68
69
namespace WucGalleryApp;
public partial class App : Application
{
public static Window MainWindow = Window.Current;
public IServiceProvider Services { get; }
public new static App Current => (App)Application.Current;
public IJsonNavigationViewService GetJsonNavigationViewService => GetService<IJsonNavigationViewService>();
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();
this.InitializeComponent();
}
private static IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddSingleton<IThemeService, ThemeService>();
services.AddSingleton<IJsonNavigationViewService>(factory =>
{
var json = new JsonNavigationViewService();
json.ConfigDefaultPage(typeof(HomeLandingPage));
json.ConfigSettingsPage(typeof(SettingsPage));
json.ConfigSectionPage(typeof(DemoSectionPage));
return json;
});
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);
}
rootFrame.Navigate(typeof(MainPage));
MainWindow.Title = MainWindow.AppWindow.Title = ProcessInfoHelper.ProductNameAndVersion;
MainWindow.AppWindow.SetIcon("Assets/icon.ico");
MainWindow.Activate();
}
}