Prism(六) 事件聚合器
源码地址 - WineMonk/PrismStudy: Prism框架学习 (github.com)
事件聚合器
事件发布模块
MessageView.xaml
1 2 3 4 5 6 7 8 9 10
| <UserControl x:Class="ModuleA.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 Text="{Binding Message}" Margin="5"/> <Button Command="{Binding SendMessageCommand}" Content="Send Message" Margin="5"/> </StackPanel> </UserControl>
|
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
| public class MessageViewModel : BindableBase { IEventAggregator _ea;
private string _message = "Message to Send"; public string Message { get { return _message; } set { SetProperty(ref _message, value); } }
public DelegateCommand SendMessageCommand { get; private set; }
public MessageViewModel(IEventAggregator ea) { _ea = ea; SendMessageCommand = new DelegateCommand(SendMessage); }
private void SendMessage() { _ea.GetEvent<MessageSentEvent>().Publish(Message); } }
|
ModuleAModule.cs
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class ModuleAModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { var regionManager = containerProvider.Resolve<IRegionManager>(); regionManager.RegisterViewWithRegion("LeftRegion", typeof(MessageView)); }
public void RegisterTypes(IContainerRegistry containerRegistry) { } }
|
事件订阅模块
MessageList.xaml
1 2 3 4 5 6 7 8 9
| <UserControl x:Class="ModuleB.Views.MessageList" 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 Messages}" /> </Grid> </UserControl>
|
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
| public class MessageListViewModel : BindableBase { IEventAggregator _ea;
private ObservableCollection<string> _messages; public ObservableCollection<string> Messages { get { return _messages; } set { SetProperty(ref _messages, value); } }
public MessageListViewModel(IEventAggregator ea) { _ea = ea; Messages = new ObservableCollection<string>();
_ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived); }
private void MessageReceived(string message) { Messages.Add(message); } }
|
ModuleBModule.cs
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class ModuleBModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { var regionManager = containerProvider.Resolve<IRegionManager>(); regionManager.RegisterViewWithRegion("RightRegion", typeof(MessageList)); }
public void RegisterTypes(IContainerRegistry containerRegistry) {
} }
|
信息发送事件
MessageSentEvent.cs
1 2 3
| public class MessageSentEvent : PubSubEvent<string> { }
|
主程序
MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <Window x:Class="UsingEventAggregator.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" Title="{Binding Title}" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <ContentControl prism:RegionManager.RegionName="LeftRegion" /> <ContentControl Grid.Column="1" prism:RegionManager.RegionName="RightRegion" /> </Grid> </Window>
|
App.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); }
protected override void RegisterTypes(IContainerRegistry containerRegistry) { }
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { moduleCatalog.AddModule<ModuleA.ModuleAModule>(); moduleCatalog.AddModule<ModuleB.ModuleBModule>(); } }
|
事件聚合器扩展:订阅事件过滤
事件订阅模块
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
| public class MessageListViewModel : BindableBase { IEventAggregator _ea;
private ObservableCollection<string> _messages; public ObservableCollection<string> Messages { get { return _messages; } set { SetProperty(ref _messages, value); } }
public MessageListViewModel(IEventAggregator ea) { _ea = ea; Messages = new ObservableCollection<string>();
_ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived, ThreadOption.PublisherThread, false, (msg) => msg.StartsWith("Zhy")); }
private void MessageReceived(string message) { Messages.Add(message); } }
|