ViewModelLocator.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Ioc;
  3. using GDNXFD.Alert.Config.Services.Navigation;
  4. using Microsoft.Practices.ServiceLocation;
  5. namespace GDNXFD.Alert.Config.ViewModel
  6. {
  7. /// <summary>
  8. /// This class contains static references to all the view models in the
  9. /// application and provides an entry point for the bindings.
  10. /// </summary>
  11. public class ViewModelLocator
  12. {
  13. /// <summary>
  14. /// Initializes a new instance of the ViewModelLocator class.
  15. /// </summary>
  16. private ViewModelLocator()
  17. {
  18. ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  19. SimpleIoc.Default.Reset();
  20. SimpleIoc.Default.Register<INavigationService, NavigationService>(true);
  21. SimpleIoc.Default.Register<MainViewModel>();
  22. SimpleIoc.Default.Register<RuleListViewModel>();
  23. SimpleIoc.Default.Register<RuleFormViewModel>();
  24. SimpleIoc.Default.Register<LoginViewModel>();
  25. }
  26. public static ViewModelLocator Instance
  27. {
  28. get { return SingletonCreator.instance; }
  29. }
  30. class SingletonCreator
  31. {
  32. internal static readonly ViewModelLocator instance = new ViewModelLocator();
  33. }
  34. public MainViewModel Main
  35. {
  36. get
  37. {
  38. return ServiceLocator.Current.GetInstance<MainViewModel>();
  39. }
  40. }
  41. public RuleListViewModel RuleList
  42. {
  43. get
  44. {
  45. return ServiceLocator.Current.GetInstance<RuleListViewModel>();
  46. }
  47. }
  48. public LoginViewModel Login
  49. {
  50. get
  51. {
  52. return ServiceLocator.Current.GetInstance<LoginViewModel>();
  53. }
  54. }
  55. public RuleFormViewModel RuleForm
  56. {
  57. get
  58. {
  59. var vm = ServiceLocator.Current.GetInstance<RuleFormViewModel>();
  60. //vm.PreInit();
  61. return vm;
  62. }
  63. }
  64. public INavigationService NavService
  65. {
  66. get
  67. {
  68. return ServiceLocator.Current.GetInstance<INavigationService>();
  69. }
  70. }
  71. public static void Cleanup()
  72. {
  73. // TODO Clear the ViewModels
  74. }
  75. }
  76. }