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();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        // 注册视图导航
        containerRegistry.RegisterForNavigation();
        containerRegistry.RegisterForNavigation();
        
        // 注册服务
        containerRegistry.Register();
        
        // 注册复合命令
        containerRegistry.RegisterSingleton();
    }

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        // 动态加载模块
        moduleCatalog.AddModule();
    }
}

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().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();
        
        // 订阅事件
        var ea = containerProvider.Resolve();
        ea.GetEvent().Subscribe(HandleMessage);
    }

    private void HandleMessage(string message)
    {
        // 处理接收到的消息
    }
}

5. 聚合事件 (MessageSentEvent.cs)

public class MessageSentEvent : PubSubEvent { }

6. Shell视图导航 (ShellView.xaml)


    
        
            
                
                
            
        
        
        
        
    

7. 模块视图 (ModuleAView.xaml)


    
        

功能说明

  1. 依赖注入:通过Bootstrapper自动注册所有组件
  2. 导航:使用RegionManager管理内容区域导航
  3. 复合命令SaveAllCommand可同时触发多个模块的保存操作
  4. 模块化ModuleAModule实现按需加载
  5. 聚合事件MessageSentEvent实现模块间松耦合通信

此项目完整展示了Prism的核心功能集成,可直接扩展为实际企业级应用架构。所有组件通过依赖注入解耦,支持模块化开发和功能扩展。

你可能感兴趣的:(wpf,c#,visual,studio,开发语言)