■ IEventAggregator 인터페이스를 사용해 이벤트 구독시 이벤트를 필터링하는 방법을 보여준다.
[TestLibrary 프로젝트]
▶ MessageSentEvent.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using Prism.Events; namespace TestLibrary { /// <summary> /// 메시지 발송시 이벤트 /// </summary> public class MessageSentEvent : PubSubEvent<string> { } } |
[TestModule1 프로젝트]
▶ MessageView.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<UserControl x:Class="TestModule1.Views.MessageView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Padding="25"> <StackPanel> <TextBox Margin="10" Padding="5" Text="{Binding Message}" /> <Button Margin="10" Padding="5" Command="{Binding SendMessageCommand}" Content="Send Message" /> </StackPanel> </UserControl> |
▶ MessageView.xaml.cs
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 |
using System.Windows.Controls; namespace TestModule1.Views { /// <summary> /// 메시지 뷰 /// </summary> public partial class MessageView : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MessageView() /// <summary> /// 생성자 /// </summary> public MessageView() { InitializeComponent(); } #endregion } } |
▶ MessageViewModel.cs
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
using Prism.Commands; using Prism.Events; using Prism.Mvvm; using TestLibrary; namespace TestModule1.ViewModels { /// <summary> /// 메시지 뷰 모델 /// </summary> public class MessageViewModel : BindableBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이벤트 수집기 /// </summary> private IEventAggregator eventAggregator; /// <summary> /// 메시지 /// </summary> private string message = "Message to Send"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메시지 - Message /// <summary> /// 메시지 /// </summary> public string Message { get { return this.message; } set { SetProperty(ref this.message, value); } } #endregion #region 메시지 송신 명령 - SendMessageCommand /// <summary> /// 메시지 송신 명령 /// </summary> public DelegateCommand SendMessageCommand { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MessageViewModel(eventAggregator) /// <summary> /// 생성자 /// </summary> /// <param name="eventAggregator">이벤트 수집기</param> public MessageViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; SendMessageCommand = new DelegateCommand(SendMessage); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 메시지 송신하기 - SendMessage() /// <summary> /// 메시지 송신하기 /// </summary> private void SendMessage() { this.eventAggregator.GetEvent<MessageSentEvent>().Publish(Message); } #endregion } } |
▶ TestModule1Module.cs
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 |
using Prism.Ioc; using Prism.Modularity; using Prism.Regions; using TestModule1.Views; namespace TestModule1 { /// <summary> /// 테스트 모듈 1 모듈 /// </summary> public class TestModule1Module : IModule { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 초기화시 처리하기 - OnInitialized(containerProvider) /// <summary> /// 초기화시 처리하기 /// </summary> /// <param name="containerProvider">컨테이너 공급자 인터페이스</param> public void OnInitialized(IContainerProvider containerProvider) { IRegionManager regionManager = containerProvider.Resolve<IRegionManager>(); regionManager.RegisterViewWithRegion("LeftRegion", typeof(MessageView)); } #endregion #region 타입 등록하기 - RegisterTypes(containerRegistry) /// <summary> /// 타입 등록하기 /// </summary> /// <param name="containerRegistry">컨테이너 레지스트리 인터페이스</param> public void RegisterTypes(IContainerRegistry containerRegistry) { } #endregion } } |
[TestModule2 프로젝트]
▶ MessageListView.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<UserControl x:Class="TestModule2.Views.MessageListView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Padding="25"> <Grid> <ListBox ItemsSource="{Binding MessageCollection}" /> </Grid> </UserControl> |
▶ MessageListView.xaml.cs
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 |
using System.Windows.Controls; namespace TestModule2.Views { /// <summary> /// 메시지 리스트 뷰 /// </summary> public partial class MessageListView : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MessageListView() /// <summary> /// 생성자 /// </summary> public MessageListView() { InitializeComponent(); } #endregion } } |
▶ MessageListViewModel.cs
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
using System.Collections.ObjectModel; using Prism.Events; using Prism.Mvvm; using TestLibrary; namespace TestModule2.ViewModels { /// <summary> /// 메시지 리스트 뷰 모델 /// </summary> public class MessageListViewModel : BindableBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이벤트 수집기 /// </summary> private IEventAggregator eventAggregator; /// <summary> /// 메시지 컬렉션 /// </summary> private ObservableCollection<string> messageCollection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메시지 컬렉션 - MessageCollection /// <summary> /// 메시지 컬렉션 /// </summary> public ObservableCollection<string> MessageCollection { get { return this.messageCollection; } set { SetProperty(ref this.messageCollection, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MessageListViewModel(eventAggregator) /// <summary> /// 생성자 /// </summary> /// <param name="eventAggregator">이벤트 수집기</param> public MessageListViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; MessageCollection = new ObservableCollection<string>(); this.eventAggregator.GetEvent<MessageSentEvent>().Subscribe ( MessageReceived, ThreadOption.PublisherThread, false, (filter) => filter.Contains("Brian") ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 메시지 수신시 처리하기 - MessageReceived(message) /// <summary> /// 메시지 수신시 처리하기 /// </summary> /// <param name="message">메시지</param> private void MessageReceived(string message) { MessageCollection.Add(message); } #endregion } } |
▶ TestModule2Module.cs
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 |
using Prism.Ioc; using Prism.Modularity; using Prism.Regions; using TestModule2.Views; namespace TestModule2 { /// <summary> /// 테스트 모듈 2 모듈 /// </summary> public class TestModule2Module : IModule { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 초기화시 처리하기 - OnInitialized(containerProvider) /// <summary> /// 초기화시 처리하기 /// </summary> /// <param name="containerProvider">컨테이너 공급자 인터페이스</param> public void OnInitialized(IContainerProvider containerProvider) { IRegionManager regionManager = containerProvider.Resolve<IRegionManager>(); regionManager.RegisterViewWithRegion("RightRegion", typeof(MessageListView)); } #endregion #region 타입 등록하기 - RegisterTypes(containerRegistry) /// <summary> /// 타입 등록하기 /// </summary> /// <param name="containerRegistry">컨테이너 레지스트리 인터페이스</param> public void RegisterTypes(IContainerRegistry containerRegistry) { } #endregion } } |
[TestProject 프로젝트]
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<Window x:Class="TestProject.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Width="800" Height="600" Title="{Binding Title}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <ContentControl Grid.Column="0" prism:RegionManager.RegionName="LeftRegion" /> <ContentControl Grid.Column="1" prism:RegionManager.RegionName="RightRegion" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
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 |
using System.Windows; namespace TestProject.Views { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion } } |
▶ MainWindowViewModel.cs
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 52 53 54 55 56 57 58 59 60 |
using Prism.Mvvm; namespace TestProject.ViewModels { /// <summary> /// 메인 윈도우 뷰 모델 /// </summary> public class MainWindowViewModel : BindableBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 제목 /// </summary> private string title = "IEventAggregator 인터페이스 : 이벤트 구독시 이벤트 필터링하기"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get { return this.title; } set { SetProperty(ref this.title, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindowViewModel() /// <summary> /// 생성자 /// </summary> public MainWindowViewModel() { } #endregion } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 |
<prism:PrismApplication x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/"> </prism:PrismApplication> |
▶ MainApplication.xaml.cs
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 52 53 54 55 56 57 58 |
using System.Windows; using Prism.Ioc; using Prism.Modularity; using Prism.Unity; using TestProject.Views; namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : PrismApplication { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 쉘 생성하기 - CreateShell() /// <summary> /// 쉘 생성하기 /// </summary> /// <returns>쉘</returns> protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } #endregion #region 타입 등록하기 - RegisterTypes(containerRegistry) /// <summary> /// 타입 등록하기 /// </summary> /// <param name="containerRegistry">컨테이너 레지스트리 인터페이스</param> protected override void RegisterTypes(IContainerRegistry containerRegistry) { } #endregion #region 모듈 카탈로그 구성하기 - ConfigureModuleCatalog(moduleCatalog) /// <summary> /// 모듈 카탈로그 구성하기 /// </summary> /// <param name="moduleCatalog">모듈 카탈로그</param> protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { moduleCatalog.AddModule<TestModule1.TestModule1Module>(); moduleCatalog.AddModule<TestModule2.TestModule2Module>(); } #endregion } } |