ViewModelLocator.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. In App.xaml:
  3. <Application.Resources>
  4. <vm:ViewModelLocator xmlns:vm="clr-namespace:MuchInfo.Chart.App.ViewModels"
  5. x:Key="Locator" />
  6. </Application.Resources>
  7. In the View:
  8. DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  9. */
  10. using GalaSoft.MvvmLight;
  11. using GalaSoft.MvvmLight.Ioc;
  12. using Microsoft.Practices.ServiceLocation;
  13. using MuchInfo.Chart.App.Services;
  14. namespace MuchInfo.Chart.App.ViewModels
  15. {
  16. /// <summary>
  17. /// This class contains static references to all the view models in the
  18. /// application and provides an entry point for the bindings.
  19. /// <para>
  20. /// See http://www.galasoft.ch/mvvm
  21. /// </para>
  22. /// </summary>
  23. public class ViewModelLocator
  24. {
  25. static ViewModelLocator()
  26. {
  27. ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  28. //注册服务
  29. if (ViewModelBase.IsInDesignModeStatic)
  30. {
  31. //in Blend
  32. // SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
  33. }
  34. else
  35. {
  36. //In Visual Studio
  37. // SimpleIoc.Default.Register<IDataService, DataService>();
  38. SimpleIoc.Default.Register<IDataService, DataService>();
  39. }
  40. SimpleIoc.Default.Register<MainViewModel>();
  41. }
  42. /// <summary>
  43. /// Gets the Main property.
  44. /// </summary>
  45. public MainViewModel Main
  46. {
  47. get
  48. {
  49. return ServiceLocator.Current.GetInstance<MainViewModel>();
  50. }
  51. }
  52. }
  53. }