Prism框架实战:WPF企业级开发全解
以下是一个完整的WPF项目示例,使用Prism框架实现依赖注入、导航、复合命令、模块化和聚合事件功能。项目结构清晰,包含核心功能实现:
项目结构
PrismDemoApp/
├── PrismDemoApp (主项目)
│ ├── Views/
│ │ ├── ShellView.xaml
│ │ ├── MainView.xaml
│ │ └── SettingsView.xaml
│ ├── ViewModels/
│ │ ├── ShellViewModel.cs
│ │ ├── MainViewModel.cs
│ │ └── SettingsViewModel.cs
│ ├── App.xaml
│ └── Bootstrapper.cs
├── ModuleA (模块项目)
│ ├── Views/
│ │ └── ModuleAView.xaml
│ ├── ViewModels/
│ │ └── ModuleAViewModel.cs
│ └── ModuleAModule.cs
└── Events/└── MessageSentEvent.cs
1. 依赖注入配置 (Bootstrapper.cs)
public class Bootstrapper : PrismBootstrapper
{protected override DependencyObject CreateShell(){return Container.Resolve<ShellView>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){// 注册视图导航containerRegistry.RegisterForNavigation<MainView>();containerRegistry.RegisterForNavigation<SettingsView>();// 注册服务containerRegistry.Register<IDataService, DataService>();// 注册复合命令containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();}protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){// 动态加载模块moduleCatalog.AddModule<ModuleAModule>();}
}
2. 复合命令实现 (ApplicationCommands.cs)
public interface IApplicationCommands
{CompositeCommand SaveAllCommand { get; }
}public class ApplicationCommands : IApplicationCommands
{public CompositeCommand SaveAllCommand { get; } = new CompositeCommand();
}
3. 主视图模型 (MainViewModel.cs)
public class MainViewModel : BindableBase
{private readonly IRegionManager _regionManager;private readonly IEventAggregator _eventAggregator;private readonly IApplicationCommands _commands;public DelegateCommand NavigateCommand { get; }public DelegateCommand SaveCommand { get; }public DelegateCommand SendEventCommand { get; }public MainViewModel(IRegionManager regionManager,IEventAggregator eventAggregator,IApplicationCommands commands){_regionManager = regionManager;_eventAggregator = eventAggregator;_commands = commands;NavigateCommand = new DelegateCommand(NavigateToSettings);SaveCommand = new DelegateCommand(Save);SendEventCommand = new DelegateCommand(SendEvent);// 注册到复合命令commands.SaveAllCommand.RegisterCommand(SaveCommand);}private void NavigateToSettings(){_regionManager.RequestNavigate("ContentRegion", "SettingsView");}private void Save(){// 保存逻辑}private void SendEvent(){_eventAggregator.GetEvent<MessageSentEvent>().Publish("Hello from Main!");}
}
4. 模块实现 (ModuleAModule.cs)
public class ModuleAModule : IModule
{private readonly IRegionManager _regionManager;private readonly IApplicationCommands _commands;public ModuleAModule(IRegionManager regionManager, IApplicationCommands commands){_regionManager = regionManager;_commands = commands;}public void OnInitialized(IContainerProvider containerProvider){_regionManager.RegisterViewWithRegion("ModuleRegion", typeof(ModuleAView));}public void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterForNavigation<ModuleAView>();// 订阅事件var ea = containerProvider.Resolve<IEventAggregator>();ea.GetEvent<MessageSentEvent>().Subscribe(HandleMessage);}private void HandleMessage(string message){// 处理接收到的消息}
}
5. 聚合事件 (MessageSentEvent.cs)
public class MessageSentEvent : PubSubEvent<string> { }
6. Shell视图导航 (ShellView.xaml)
<Window xmlns:prism="http://prismlibrary.com/"><DockPanel><Menu><MenuItem Header="导航"><MenuItem Command="{Binding NavigateCommand}" Header="主视图" /><MenuItem prism:CommandBehavior.Command="{Binding ApplicationCommands.SaveAllCommand}" Header="保存所有" /></MenuItem></Menu><ContentControl prism:RegionManager.RegionName="ContentRegion" /><ContentControl prism:RegionManager.RegionName="ModuleRegion" /></DockPanel>
</Window>
7. 模块视图 (ModuleAView.xaml)
<UserControl><StackPanel><Button Command="{Binding SendEventCommand}" Content="发送事件" /><TextBlock Text="{Binding ReceivedMessage}" /></StackPanel>
</UserControl>
功能说明
- 依赖注入:通过
Bootstrapper
自动注册所有组件 - 导航:使用
RegionManager
管理内容区域导航 - 复合命令:
SaveAllCommand
可同时触发多个模块的保存操作 - 模块化:
ModuleAModule
实现按需加载 - 聚合事件:
MessageSentEvent
实现模块间松耦合通信
此项目完整展示了Prism的核心功能集成,可直接扩展为实际企业级应用架构。所有组件通过依赖注入解耦,支持模块化开发和功能扩展。