ViewModelLocator.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. In App.xaml:
  3. <Application.Resources>
  4. <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:Editor.ViewModel"
  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 Editor.Model;
  14. namespace Editor.ViewModel
  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. /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
  21. /// to this locator.
  22. /// </para>
  23. /// <para>
  24. /// See http://www.galasoft.ch/mvvm/getstarted
  25. /// </para>
  26. /// </summary>
  27. public class ViewModelLocator
  28. {
  29. static ViewModelLocator()
  30. {
  31. ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  32. if (ViewModelBase.IsInDesignModeStatic)
  33. {
  34. SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
  35. }
  36. else
  37. {
  38. SimpleIoc.Default.Register<IDataService, DataService>();
  39. }
  40. SimpleIoc.Default.Register<MainViewModel>();
  41. }
  42. /// <summary>
  43. /// Gets the Main property.
  44. /// </summary>
  45. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
  46. "CA1822:MarkMembersAsStatic",
  47. Justification = "This non-static member is needed for data binding purposes.")]
  48. public MainViewModel Main
  49. {
  50. get
  51. {
  52. return ServiceLocator.Current.GetInstance<MainViewModel>();
  53. }
  54. }
  55. /// <summary>
  56. /// Cleans up all the resources.
  57. /// </summary>
  58. public static void Cleanup()
  59. {
  60. }
  61. }
  62. }