App.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Microsoft.EntityFrameworkCore;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using UniformMaterialManagementSystem.Data;
  6. using UniformMaterialManagementSystem.Models;
  7. using UniformMaterialManagementSystem.Services;
  8. using UniformMaterialManagementSystem.ViewModels;
  9. using UniformMaterialManagementSystem.Views;
  10. using UniformMaterialManagementSystem.Views.LoginAndRegister;
  11. namespace UniformMaterialManagementSystem
  12. {
  13. public partial class App
  14. {
  15. public IServiceProvider Services { get; }
  16. public new static App Current => (App)Application.Current;
  17. public static LoginUser? CurrentUser { get; private set; }
  18. public App()
  19. {
  20. Services = ConfigureServices();
  21. this.InitializeComponent();
  22. using var dbContext = new SqliteContext();
  23. dbContext.Database.Migrate();
  24. }
  25. protected override void OnStartup(StartupEventArgs e)
  26. {
  27. //文本框Binding双向绑定了数据Text=“{UpdateSourceTrigger=PropertyChanged}”,但数据是double类型,手动输入数据时,小数点输入不进去,设置此属性
  28. FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
  29. base.OnStartup(e);
  30. }
  31. private static IServiceProvider ConfigureServices()
  32. {
  33. var services = new ServiceCollection();
  34. // DbContext 注册
  35. services.AddDbContext<SqliteContext>();
  36. // View 注册
  37. services.AddSingleton<MainWindow>();
  38. services.AddTransient<LoginPage>();
  39. services.AddTransient<RegisterPage>();
  40. services.AddSingleton<LoginAndRegisterWindow>();
  41. services.AddKeyedTransient<UserControl, HomePage>("HomePage");
  42. services.AddKeyedTransient<UserControl, MenuPage>("MenuPage");
  43. services.AddKeyedTransient<UserControl, SupervisionUnitPage>("SupervisionUnitPage");
  44. services.AddKeyedTransient<UserControl, MaterialCompany>("MaterialCompany");
  45. services.AddKeyedTransient<UserControl, ProductCompanyPage>("ProductCompany");
  46. services.AddKeyedTransient<UserControl, RolePage>("RolePage");
  47. services.AddKeyedTransient<UserControl, MaterialControl>("MaterialControl");
  48. services.AddKeyedTransient<UserControl, PersonalInfoPage>("PersonalInfoPage");
  49. services.AddKeyedTransient<UserControl, InspectApplyPage>("InspectApplyPage");
  50. services.AddKeyedTransient<UserControl, UsersControl>("UsersControl");
  51. services.AddKeyedTransient<UserControl, InspectionRecordPage>("InspectionRecordPage");
  52. services.AddKeyedTransient<UserControl, ContractPage>("ContractPage");
  53. services.AddKeyedTransient<UserControl, DeliveryReceiptControl>("DeliveryReceiptControl");
  54. services.AddKeyedTransient<UserControl, InspectionReportPage>("InspectionReportPage");
  55. services.AddKeyedTransient<UserControl, SampleRegistrationPage>("SampleRegistrationPage");
  56. services.AddKeyedTransient<UserControl, SelectApplyContractDialog>("SelectApplyContractDialog");
  57. services.AddKeyedTransient<UserControl, InspectionOrganizationPage>("InspectionOrganizationPage");
  58. services.AddKeyedTransient<UserControl, InspectionOrganizationPage>("InspectionOrganizationPage");
  59. services.AddKeyedTransient<UserControl, FactoryLicensePage>("FactoryLicensePage");
  60. // ViewModel 注册
  61. services.AddSingleton<MainWindowViewModel>();
  62. services.AddSingleton<LoginAndRegisterViewModel>();
  63. services.AddTransient<LoginPageViewModel>();
  64. services.AddTransient<RegisterPageViewModel>();
  65. services.AddTransient<HomePageViewModel>();
  66. services.AddTransient<MenuPageViewModel>();
  67. services.AddTransient<SupervisionUnitPageViewModel>();
  68. services.AddTransient<MaterialCompanyViewModel>();
  69. services.AddTransient<ProductCompanyPageViewModel>();
  70. services.AddTransient<RolePageViewModel>();
  71. services.AddTransient<MaterialViewModel>();
  72. services.AddTransient<PersonalInfoPageViewModel>();
  73. services.AddTransient<InspectApplyPageViewModel>();
  74. services.AddTransient<UsersViewModel>();
  75. services.AddTransient<SelectContractWindowViewModel>();
  76. services.AddTransient<InspectionRecordPageViewModel>();
  77. services.AddTransient<ContractPageViewModel>();
  78. services.AddTransient<DeliveryReceiptViewModel>();
  79. services.AddTransient<InspectionReportPageViewModel>();
  80. services.AddTransient<SampleRegistrationPageViewModel>();
  81. services.AddTransient<InspectionOrganizationPageViewModel>();
  82. services.AddTransient<FactoryLicensePageViewModel>();
  83. services.AddTransient<SelectApplyContractDialogViewModel>();
  84. services.AddTransient<InspectionOrganizationPageViewModel>();
  85. // Service 注册
  86. services.AddScoped(typeof(IDataBaseService<>), typeof(DataBaseService<>));
  87. return services.BuildServiceProvider();
  88. }
  89. internal static void SetCurrentUser(LoginUser? user)
  90. {
  91. CurrentUser = user;
  92. }
  93. }
  94. }