窗体
- 作者仓库星标 423
- 作者仓库 dotnet-skills
- 领域
- 设计与多媒体
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @managedcode · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 即装即用
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- Windows
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# Windows Forms
## Trigger On
- working on Windows Forms UI, event-driven workflows, or classic LOB applications
- migrating WinForms from .NET Framework to modern .NET
- cleaning up oversized form code or designer coupling
- implementing data binding, validation, or control customization
## Workflow
1. **Respect designer boundaries** — never edit `.Designer.cs` directly; changes are lost on regeneration.
2. **Separate business logic from forms** — use MVP (Model-View-Presenter) pattern. Forms orchestrate UI; presenters contain logic; services handle data access.
```csharp
// View interface — forms implement this
public interface ICustomerView
{
string CustomerName { get; set; }
event EventHandler SaveRequested;
void ShowError(string message);
}
// Presenter — testable without UI
public class CustomerPresenter
{
private readonly ICustomerView _view;
private readonly ICustomerService _service;
public CustomerPresenter(ICustomerView view, ICustomerService service)
{
_view = view;
_service = service;
_view.SaveRequested += async (s, e) =>
{
try { await _service.SaveAsync(_view.CustomerName); }
catch (Exception ex) { _view.ShowError(ex.Message); }
};
}
}
```
3. **Use DI from Program.cs** (.NET 6+):
```csharp
var services = new ServiceCollection();
services.AddSingleton<ICustomerService, CustomerService>();
services.AddTransient<MainForm>();
using var sp = services.BuildServiceProvider();
Application.Run(sp.GetRequiredService<MainForm>());
```
4. **Use data binding** via `BindingSource` and `INotifyPropertyChanged` instead of manual control population. See [references/patterns.md](references/patterns.md) for complete binding patterns.
5. **Use async/await** for I/O operations — disable controls during loading, use `Progress<T>` for progress reporting. Never block the UI thread.
6. **Validate with `ErrorProvider`** and the `Validating` event. Call `ValidateChildren()` before save operations.
7. **Modernize incrementally** — prefer better structure over big-bang rewrites. Use .NET 8+ features (button commands, stock icons) when available.
```mermaid
flowchart LR
A["Form event"] --> B["Presenter handles logic"]
B --> C["Service layer / data access"]
C --> D["Update view via interface"]
D --> E["Validate and display results"]
```
## Key Decisions
| Decision | Guidance |
|----------|----------|
| MVP vs MVVM | Prefer MVP for WinForms — simpler with event-driven model |
| BindingSource vs manual | Always prefer BindingSource for list/detail binding |
| Sync vs async I/O | Always async — use `async void` only for event handlers |
| Custom controls | Extract reusable `UserControl` when form grows beyond ~300 lines |
| .NET Framework → .NET | Use the official migration guide; validate designer compatibility first |
## Deliver
- less brittle form code with clear UI/logic separation
- MVP pattern with testable presenters
- pragmatic modernization guidance for WinForms-heavy apps
- data binding and validation patterns that reduce manual wiring
## Validate
- designer files stay stable and are not hand-edited
- forms are not acting as the application service layer
- async operations do not block the UI thread
- validation is implemented consistently with ErrorProvider
- Windows-only runtime behavior is tested on target
## References
- [references/patterns.md](references/patterns.md) - WinForms architectural patterns (MVP, MVVM, Passive View), data binding, validation, form communication, threading, DI setup, and .NET 8+ features
- [references/migration.md](references/migration.md) - step-by-step migration from .NET Framework to modern .NET, common issues, deployment options, and gradual migration strategies
章节与文件索引
按说明文件中的顺序排列。
触发
工作流程
关键决策
交付
验证
文件/命令 -> .Designer.cs · BindingSource · INotifyPropertyChanged · Progress<T> · ErrorProvider · Validating · ValidateChildren() · async void
内容校验值 -> 5346fe3182cf
当前原文的自动摘译,尚未人工审校。
触发
处理 Windows 窗体 UI、事件驱动工作流或经典 LOB 应用程序 将 WinForms 从 .NET Framework 迁移到现代 .NET 清理超大表单代码或设计器耦合,实现数据绑定、验证或控件自定义
工作流程
尊重设计师的界限——永远不要直接编辑
.Designer.cs;再生时变化会丢失。将业务逻辑与表单分离——使用 MVP(模型-视图-演示者)模式。表单编排 UI;演讲者有逻辑;服务处理数据访问。使用 Program.cs (.NET 6+) 中的 DI:通过BindingSource和INotifyPropertyChanged使用数据绑定,而不是手动控制填充。有关完整的绑定模式,请参阅references/patterns.md。关键决策
决策 · 指导 MVP 与 MVVM · 更喜欢 WinForms 的 MVP — 使用事件驱动模型更简单 BindingSource 与手动 · 始终更喜欢 BindingSource 进行列表/详细信息绑定 同步 vs 异步 I/O · 始终异步 — 仅将
async void用于事件处理程序 自定义控件 · 当表单增长超过约 300 行时,提取可重用的UserControl交付
具有清晰 UI/逻辑分离的脆弱表单代码 具有可测试演示者的 MVP 模式 针对 WinForms 密集型应用程序的实用现代化指导 数据绑定和验证模式可减少手动接线
验证
设计器文件保持稳定并且不是手工编辑的表单不会充当应用程序服务层异步操作不会阻止 UI 线程验证与 ErrorProvider 一致地实现仅 Windows 运行时行为在目标上进行测试