Saury.Revit.Template 1.0.1

dotnet new install Saury.Revit.Template::1.0.1
                    
This package contains a .NET Template Package you can call from the shell/command line.

Saury.Revit.Template

一个基于 MVVM 模式和依赖注入的 Revit 2026 插件开发模板。

特性

  • MVVM 架构 - 使用 CommunityToolkit.Mvvm 实现完整的 Model-View-ViewModel 模式
  • 依赖注入 - 基于 Microsoft.Extensions.Hosting 的 DI 容器
  • 日志系统 - 集成 Serilog,支持文件和控制台输出
  • 自动替换 - 项目名称、命名空间、GUID 自动生成和替换
  • 自动部署 - 编译后自动复制到 Revit 插件目录
  • 完整示例 - 包含 AboutView 作为 MVVM 参考实现

安装模板

从 NuGet 安装(推荐)

dotnet new install Saury.Revit.Template

使用模板

创建新项目

dotnet new saury-revit -n MyRevitPlugin

卸载模板

dotnet new uninstall Saury.Revit.Template

项目结构

MyRevitPlugin/
├── Commands/          # Revit 命令
│   └── AboutCommand.cs
├── Models/           # 数据模型
│   └── AboutInfo.cs
├── ViewModels/       # 视图模型(使用 CommunityToolkit.Mvvm)
│   └── AboutViewModel.cs
├── Views/            # WPF 视图
│   └── AboutView.xaml
├── Services/         # 服务层
│   └── AssemblyResolver.cs
├── Extensions/       # 扩展方法
│   └── DataContextExtension.cs
├── Resources/        # 资源文件
│   └── Icons/
├── Host.cs          # DI 容器配置
└── Application.cs   # Revit 插件入口

MVVM 架构说明

Model

数据模型和业务逻辑,纯 C# 类。

public class AboutInfo
{
    public string ProjectName { get; set; }
    public string Version { get; set; }
}

ViewModel

继承 ObservableObject,使用 [ObservableProperty][RelayCommand]

public partial class AboutViewModel : ObservableObject
{
    [ObservableProperty]
    private string version;

    // UIApplication 通过 DI 自动注入
    public AboutViewModel(UIApplication? uiApp = null) { }
}

View

XAML 界面,通过构造函数注入 ViewModel。

public AboutView(AboutViewModel viewModel)
{
    InitializeComponent();
    DataContext = viewModel;
}

Command

从 DI 容器获取 View 并显示。

public Result Execute(...)
{
    var view = Host.GetService<Views.AboutView>();
    view.ShowDialog();
    return Result.Succeeded;
}

依赖注入

所有服务在 Host.cs 中注册:

// 注册 UIApplication(Revit 上下文)
builder.Services.AddSingleton(uiApplication);

// 注册 ViewModel 和 View
builder.Services.AddTransient<ViewModels.AboutViewModel>();
builder.Services.AddTransient<Views.AboutView>();

开发指南

  1. 添加新功能

    • 创建 Model(数据模型)
    • 创建 ViewModel(业务逻辑)
    • 创建 View(界面)
    • 创建 Command(Revit 命令)
    • 在 Host.cs 中注册服务
  2. 使用日志

    var logger = Host.GetService<ILogger<YourClass>>();
    logger.LogInformation("消息");
    
  3. 访问 Revit 上下文

    public YourViewModel(UIApplication? uiApp = null)
    {
        _uiApp = uiApp; // 自动注入
    }
    

技术栈

  • .NET 8.0
  • WPF
  • CommunityToolkit.Mvvm 8.4.0
  • Microsoft.Extensions.Hosting 9.0.4
  • Serilog 9.0.0
  • Revit 2026 API

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 252 1/11/2026
1.0.0 240 1/11/2026