当前位置: 首页 > news >正文

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>

功能说明

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

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

相关文章:

  • 从萌芽到领航:广州华锐互动的 AR 奋进之路​
  • oracle内存参数调整
  • 免安装一键修复网络诊断 + 权限修复!打印机共享错误工具适配 Win7/10/11
  • 集群聊天服务器---muduo库(3)
  • PHP Protobuf 手写生成器,
  • Redis集群实现方式
  • 鸿蒙HarmonyOS 关于图片、视频的选择详解
  • react ant-design通用页面自适应适配不同分辨率屏幕的方法工具类
  • 2025年消防设施安全员考试新增了哪些内容?重点考什么设备?
  • AiPy实战(5):效率革命!5分钟构建行业分析报告
  • SpringBoot -- 整合Junit
  • flink同步kafka到paimon,doris加速查询
  • 知名流体控制解决方案供应商“永盛科技”与商派ShopeX达成B2B商城项目合作
  • 【LeetCode】滑动窗口相关算法题
  • Re:从零开始的文件分配方式(考研向)
  • 深度学习实战:案例解析
  • 因果森林(R包grf)-治疗异质性探索
  • 三阶落地:腾讯云Serverless+Spring Cloud的微服务实战架构
  • 人工智能训练师——智能语音识别ASR
  • jvm简单八股