以下是一個完整的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的核心功能集成,可直接擴展為實際企業級應用架構。所有組件通過依賴注入解耦,支持模塊化開發和功能擴展。