WPF에서 Prism을 사용하여 ViewModel을 구현할 때 필요에 따라 Container, EventAggregator, RegionManager, Logger 등의 서비스를 사용합니다.
서비스를 각각 ViewModel 에 정의해서 사용하는 것이 아닌 공통으로 작성하여 편리하게 사용할 수 있는 추상 클래스를 작성하였습니다.
 ViewModelBase Class
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
   | using Prism.Events; using Prism.Logging; using Prism.Ioc; using Prism.Mvvm; using Unity;
  namespace Prism.Project.Common.Mvvm {     public abstract class ViewModelBase : BindableBase     {         private string title;         public string Title         {           get => title;           set => SetProperty(ref title, value);         }
          protected IUnityContainer Container { get; }
          private IEventAggregator eventAggregator;                                    public IEventAggregator EventAggregator         {             get { return eventAggregator; }             private set { this.SetProperty<IEventAggregator>(ref this.eventAggregator, value); }         }
          private IRegionManager regionManager;                                    public IRegionManager RegionManager         {             get { return regionManager; }             private set { this.SetProperty<IRegionManager>(ref this.regionManager, value); }         }
          protected ILoggerFacade Logger { get; }
          protected ViewModelBase(IUnityContainer container)         {             Container = container;             RegionManager = container.Resolve<IRegionManager>();             EventAggregator = container.Resolve<IEventAggregator>();
              Logger = container.Resolve<ILoggerFacade>();         }     } }
   | 
 
 사용 코드
ViewModelBase 클래스를 상속받고 생성자에 container 를 정의합니다.
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
   | using Prism.Project.Common.Mvvm; using Unity; using Prism.Events;
  namespace Prism.Project.ViewModels {     public class MainWindowViewModel : ViewModelBase     {         public MainWindowViewModel(IUnityContainer container) : base(container)         {             Title = "Prism 테스트";
              Logger.Log("[MainWindowViewModel Created]", Category.Debug, Priority.None);
                           EventAggregator.GetEvent<MainWindowClosedEvent>().Subscribe(MainWindowClosed);         }
                                      private void MainWindowClosed()         {         }     } }
   |