Windows 与 Presenters
Source/Windows:
| 项目 | 功能 |
|---|---|
H.Windows.Main | 主窗口、窗口命令、窗口设置保存。 |
H.Windows.Dialog | 对话窗口。 |
H.Windows.Dock | Dock 窗口。 |
H.Windows.Ribbon | Ribbon 窗口和 Ribbon 样式。 |
H.Windows.Ribbon包含 RibbonButton、RibbonTab、RibbonGroup、RibbonGallery、RibbonWindow 等,适合 Office 风格应用。
Source/Presenters:
| 项目 | 功能 |
|---|---|
H.Presenters.Common | 通用展示器。 |
H.Presenters.Repository | 仓储数据展示器。 |
H.Presenters.Design | 设计器展示器。 |
Presenter 可以理解为“业务化控件”:控件偏通用,Presenter 偏业务页面。
Windows 与 Presenters 窗口展示器详解
一、概述
在 WPF-Control 框架中,Windows负责窗口级别的容器管理,而Presenters则是"业务化控件"——介于通用控件和完整页面之间的中间层。
核心区别:控件偏通用,Presenter 偏业务页面。
二、Windows 窗口系统
2.1 窗口项目结构
Source/Windows/ ├── H.Windows.Main/ # 主窗口 ├── H.Windows.Dialog/ # 对话框窗口 ├── H.Windows.Dock/ # Dock 停靠窗口 └── H.Windows.Ribbon/ # Ribbon 窗口(Office风格)2.2 窗口类型对比
| 窗口类型 | 功能特点 | 适用场景 |
|---|---|---|
| Main | 主窗口,支持设置保存、命令绑定 | 应用主界面 |
| Dialog | 模态对话框,支持动画过渡 | 确认框、输入框 |
| Dock | 可停靠布局窗口 | 工具面板、属性面板 |
| Ribbon | Office风格带状界面 | 专业办公软件 |
2.3 DialogWindow 详解
核心功能
publicpartialclassDialogWindow:Window,IDialog{publicDialogButtonDialogButton{get;set;}=DialogButton.Sumit;publicboolUseDropShadowEffect{get;set;}publicFunc<Task<bool>>CanSumit{get;set;}publicvoidSumit(){this.DialogResult=true;this.Close();}}按钮类型
publicenumDialogButton{None,// 无按钮Sumit,// 仅确定Cancel,// 仅取消SumitAndCancel// 确定和取消}使用示例
// 方式一:直接显示 Presenterbool?result=DialogWindow.ShowPresenter(myPresenter);// 方式二:带回调的显示bool?result=DialogWindow.ShowPresenter(myPresenter,action:dialog=>{dialog.Title="自定义标题";dialog.Width=500;},canSumit:async()=>{// 验证逻辑returnawaitValidateData();});2.4 Ribbon 窗口
Ribbon 窗口适合 Office 风格应用:
<ribbon:RibbonWindowx:Class="MyApp.MainWindow"><Grid><!-- Ribbon 区域 --><ribbon:Ribbon><ribbon:RibbonTabHeader="主页"><ribbon:RibbonGroupHeader="剪贴板"><ribbon:RibbonButtonLabel="复制"LargeImageSource="..."/><ribbon:RibbonButtonLabel="粘贴"LargeImageSource="..."/></ribbon:RibbonGroup></ribbon:RibbonTab></ribbon:Ribbon><!-- 内容区域 --><ContentControl/></Grid></ribbon:RibbonWindow>三、Presenters 展示器系统
3.1 Presenter 的概念
Presenter是"业务化控件",它将:
- 控件(如 DataGrid、ListBox)
- 数据绑定
- 业务逻辑
封装成可复用的单元。
3.2 Presenter 项目结构
Source/Presenters/ ├── H.Presenters.Common/ # 通用展示器 │ ├── DataGridPresenter # 数据表格展示器 │ ├── ListBoxPresenter # 列表展示器 │ ├── TreeViewPresenter # 树形展示器 │ ├── ImageViewPresenter # 图片查看器 │ └── WaitPresenter # 等待提示器 ├── H.Presenters.Repository/ # 仓储数据展示器 └── H.Presenters.Design/ # 设计器展示器3.3 常用 Presenter
| Presenter | 功能 | 说明 |
|---|---|---|
DataGridPresenter | 数据表格展示 | 自动绑定数据源 |
ListBoxPresenter | 列表展示 | 支持多选、搜索 |
TreeViewPresenter | 树形结构展示 | 支持层级数据 |
ImageViewPresenter | 图片查看 | 支持缩放、旋转 |
WaitPresenter | 等待提示 | 加载状态显示 |
TextBoxPresenter | 文本输入 | 带验证的输入框 |
MessagePresenter | 消息展示 | 消息提示框 |
3.4 DataGridPresenter 使用示例
// 方式一:使用消息服务显示awaitIocMessage.Dialog.ShowDataGrid(presenter=>{presenter.ItemsSource=myDataList;});// 方式二:使用命令ShowDataGridCommandcommand=newShowDataGridCommand{ItemsSource=myDataList};command.Execute(null);// 方式三:XAML 绑定<ButtonCommand="{local:ShowDataGridCommand ItemsSource={Binding MyData}}"/>3.5 创建自定义 Presenter
步骤1:创建 Presenter 类
[Icon("\xE890")][Display(Name="用户列表")]publicclassUserListPresenter:BindableBase{privateObservableCollection<User>_users;publicObservableCollection<User>Users{get=>_users;set{_users=value;RaisePropertyChanged();}}privateUser_selectedUser;publicUserSelectedUser{get=>_selectedUser;set{_selectedUser=value;RaisePropertyChanged();}}publicICommandEditCommand{get;}publicICommandDeleteCommand{get;}publicUserListPresenter(){EditCommand=newRelayCommand(OnEdit);DeleteCommand=newRelayCommand(OnDelete);}privatevoidOnEdit(objectparameter){// 编辑逻辑}privatevoidOnDelete(objectparameter){// 删除逻辑}}步骤2:创建 XAML 模板
<UserControlx:Class="MyApp.UserListPresenter"><Grid><DataGridItemsSource="{Binding Users}"SelectedItem="{Binding SelectedUser}"AutoGenerateColumns="True"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Right"><ButtonCommand="{Binding EditCommand}"Content="编辑"/><ButtonCommand="{Binding DeleteCommand}"Content="删除"/></StackPanel></Grid></UserControl>步骤3:使用 Presenter
// 显示在对话框中awaitIocMessage.Dialog.ShowDialog<UserListPresenter>(presenter=>{presenter.Users=userService.GetUsers();});四、Windows 与 Presenters 的协作
4.1 架构关系
┌─────────────────────────────────────────────────────────────┐ │ Windows │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ MainWindow │ │ DialogWindow│ │ RibbonWindow│ │ │ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │ │ │ │ │ │ │ ▼ ▼ ▼ │ ├─────────────────────────────────────────────────────────────┤ │ Presenters │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │DataGridPresenter│ListBoxPresenter│TreeViewPresenter│ │ │ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │ │ │ │ │ │ │ ▼ ▼ ▼ │ ├─────────────────────────────────────────────────────────────┤ │ Controls │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ DataGrid │ │ ListBox │ │ TreeView │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘4.2 调用流程
1. 用户触发操作 │ ▼ 2. 调用 DialogWindow.ShowPresenter() │ ▼ 3. 创建 DialogWindow 实例 │ ▼ 4. 设置 Presenter 为 Content │ ▼ 5. 显示对话框 │ ▼ 6. 用户操作 Presenter 中的控件 │ ▼ 7. Presenter 的 Command/ViewModel 响应 │ ▼ 8. 点击确定/取消关闭对话框五、实际应用案例
5.1 案例一:数据选择对话框
// 创建并显示数据选择对话框publicasyncTask<User>SelectUserAsync(){UserselectedUser=null;awaitIocMessage.Dialog.ShowDialog<UserListPresenter>(presenter=>{presenter.Users=_userService.GetUsers();},presenter=>{// 确定按钮回调selectedUser=presenter.SelectedUser;});returnselectedUser;}5.2 案例二:等待提示
// 显示等待提示using(varwaitPresenter=newWaitPresenter{Message="加载中..."}){awaitIocMessage.Dialog.ShowDialog(waitPresenter,action:null);// 在后台执行耗时操作awaitTask.Run(()=>{// 耗时操作});}5.3 案例三:表单编辑
// 编辑用户信息publicasyncTask<bool>EditUserAsync(Useruser){returnawaitIocMessage.Dialog.ShowDialog<UserEditPresenter>(presenter=>{presenter.User=user;},presenter=>{// 保存逻辑_userService.Update(presenter.User);});}六、最佳实践
6.1 Presenter 设计原则
// ✅ 推荐:继承 BindableBasepublicclassMyPresenter:BindableBase{}// ✅ 推荐:使用 Display 特性[Display(Name="用户列表",Description="管理用户信息")]publicclassUserListPresenter{}// ✅ 推荐:使用 Icon 特性[Icon("\xE890")]publicclassUserListPresenter{}6.2 窗口显示方式
// ✅ 推荐:使用消息服务awaitIocMessage.Dialog.ShowDialog<MyPresenter>();// ✅ 推荐:使用静态方法DialogWindow.ShowPresenter<MyPresenter>();// ✅ 推荐:使用命令<ButtonCommand="{local:ShowMyPresenterCommand}"/>6.3 数据传递模式
// ✅ 推荐:通过回调传递数据awaitIocMessage.Dialog.ShowDialog<MyPresenter>(option:p=>p.Data=initialData,sumitAction:p=>result=p.Data);七、总结
Windows 和 Presenters 构成了 WPF-Control 的界面展示层:
- Windows提供窗口容器:主窗口、对话框、Ribbon 窗口等
- Presenters封装业务页面:数据展示、表单编辑、消息提示等
- 协作模式:Window 承载 Presenter,Presenter 使用 Controls
这种分层设计使得:
- 复用性:Presenter 可在不同窗口中复用
- 可测试性:Presenter 易于单元测试
- 可维护性:业务逻辑集中在 Presenter 中
掌握这套体系,可以快速构建专业的 WPF 应用界面。