当前位置: 首页 > 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的核心功能集成,可直接扩展为实际企业级应用架构。所有组件通过依赖注入解耦,支持模块化开发和功能扩展。

http://www.lryc.cn/news/576038.html

相关文章:

  • Greenplum
  • 鸿蒙OH南向开发 小型系统内核(LiteOS-A)【文件系统】上
  • uni-app uts 插件 android 端 科大讯飞离线语音合成最新版
  • 大模型在急性重型肝炎风险预测与治疗方案制定中的应用研究
  • 无线USB转换器TOS-WLink的无线USB助手配置文件详细胡扯
  • System.Threading.Tasks 库简介
  • Vulkan模型查看器设计:相机类与三维变换
  • Java底层原理:深入理解JVM内存模型与线程安全
  • Node.js到底是什么
  • Jmeter并发测试和持续性压测
  • IBW 2025: CertiK首席商务官出席,探讨AI与Web3融合带来的安全挑战
  • 记录一次飞书文档转md嵌入vitepress做静态站点
  • 时序数据库全面解析与对比
  • 基础RAG实现,最佳入门选择(十二)
  • mysql表操作与查询
  • RJ45 以太网与 5G 的原理解析及区别
  • 成都芯谷金融中心·文化科技产业园:绘就区域腾飞新篇章
  • 如何在安卓设备上发送长视频:6 种可行的解决方案
  • day49-硬件学习之I2C(续)
  • 数据结构之顺序表(C语言版本)
  • MongoDB 和 Redis的区别
  • Tomcat Maven 插件
  • iOS 远程调试与离线排查实战:构建非现场问题复现机制
  • K8s port、targetPort和nodePort区别
  • GitHub Actions与AWS OIDC实现安全的ECR/ECS自动化部署
  • TCP/IP协议简要概述
  • 国产鸿蒙系统开放应用侧载,能威胁到Windows地位吗?
  • 工作台-01.需求分析与设计
  • qq邮箱 新版 怎么去掉个性签名?
  • Java 大视界 -- Java 大数据在智能教育学习社群知识共享与协同学习促进中的应用(326)