# 插件开发指南 > 为 LanMountainDesktop 开发自定义插件 ## 目录 - [快速开始](#快速开始) - [插件架构](#插件架构) - [创建插件](#创建插件) - [插件生命周期](#插件生命周期) - [添加组件](#添加组件) - [添加设置页](#添加设置页) - [使用服务](#使用服务) - [打包和发布](#打包和发布) - [最佳实践](#最佳实践) ## 快速开始 ### 安装插件模板 ```bash # 安装官方插件模板 dotnet new install LanMountainDesktop.PluginTemplate # 查看可用模板 dotnet new list | findstr lmd ``` ### 创建新插件 ```bash # 创建插件项目 dotnet new lmd-plugin -n MyAwesomePlugin # 进入项目目录 cd MyAwesomePlugin # 还原依赖 dotnet restore # 构建插件 dotnet build ``` ### 项目结构 ``` MyAwesomePlugin/ ├── MyAwesomePlugin.csproj # 项目文件 ├── Plugin.cs # 插件入口 ├── Components/ # 组件目录 │ └── MyComponent.cs ├── Views/ # 视图目录 │ └── MyComponentView.axaml ├── ViewModels/ # 视图模型 │ └── MyComponentViewModel.cs ├── Settings/ # 设置页 │ └── MySettingsPage.axaml └── plugin.json # 插件清单 ``` ## 插件架构 ### 插件 SDK 版本 当前 SDK 版本: **5.0.0** ```xml ``` ### 插件清单 (plugin.json) ```json { "Id": "com.example.myawesomeplugin", "Name": "My Awesome Plugin", "Version": "1.0.0", "Author": "Your Name", "Description": "A plugin that does awesome things", "MinHostVersion": "1.0.0", "Dependencies": [], "Permissions": [ "FileSystem.Read", "Network.Access" ] } ``` ### 核心接口 **IPlugin** - 插件入口接口: ```csharp public interface IPlugin { string Id { get; } string Name { get; } string Version { get; } Task InitializeAsync(IPluginContext context); Task ShutdownAsync(); } ``` **IPluginContext** - 插件上下文: ```csharp public interface IPluginContext { string PluginDirectory { get; } IServiceProvider Services { get; } ILogger Logger { get; } ISettingsService Settings { get; } } ``` ## 创建插件 ### 1. 实现插件入口 ```csharp using LanMountainDesktop.PluginSdk; using LanMountainDesktop.Shared.Contracts; namespace MyAwesomePlugin; public class Plugin : IPlugin { public string Id => "com.example.myawesomeplugin"; public string Name => "My Awesome Plugin"; public string Version => "1.0.0"; private IPluginContext? _context; public async Task InitializeAsync(IPluginContext context) { _context = context; // 注册组件 var componentRegistry = context.Services.GetService(); componentRegistry?.RegisterComponent(); // 注册设置页 var settingsRegistry = context.Services.GetService(); settingsRegistry?.RegisterPage("我的插件设置"); // 初始化逻辑 context.Logger.LogInformation("Plugin initialized"); await Task.CompletedTask; } public async Task ShutdownAsync() { // 清理资源 _context?.Logger.LogInformation("Plugin shutting down"); await Task.CompletedTask; } } ``` ### 2. 配置项目文件 ```xml net10.0 enable enable com.example.myawesomeplugin My Awesome Plugin 1.0.0 PreserveNewest ``` ## 插件生命周期 ### 生命周期阶段 ``` 1. 发现 (Discovery) ↓ 2. 加载 (Load) ├─ 加载程序集 ├─ 验证依赖 └─ 创建插件实例 ↓ 3. 初始化 (Initialize) ├─ 调用 InitializeAsync() ├─ 注册组件 ├─ 注册设置页 └─ 初始化服务 ↓ 4. 运行 (Running) ├─ 组件渲染 ├─ 事件处理 └─ 服务调用 ↓ 5. 关闭 (Shutdown) ├─ 调用 ShutdownAsync() ├─ 清理资源 └─ 卸载程序集 ``` ### 生命周期钩子 ```csharp public class Plugin : IPlugin { // 插件加载后立即调用 public async Task InitializeAsync(IPluginContext context) { // 注册组件、服务、设置页 // 初始化资源 } // 插件卸载前调用 public async Task ShutdownAsync() { // 保存状态 // 释放资源 // 取消订阅 } } ``` ## 添加组件 ### 1. 定义组件类 ```csharp using LanMountainDesktop.PluginSdk.Components; using LanMountainDesktop.Shared.Contracts; namespace MyAwesomePlugin.Components; [Component( Id = "com.example.myawesomeplugin.mycomponent", Name = "我的组件", Description = "一个很棒的组件", Category = "工具", Icon = "avares://MyAwesomePlugin/Assets/icon.png" )] public class MyComponent : ComponentBase { public override string Id => "com.example.myawesomeplugin.mycomponent"; public override string Name => "我的组件"; // 组件设置 private string _message = "Hello, World!"; public string Message { get => _message; set => SetProperty(ref _message, value); } // 组件初始化 public override Task InitializeAsync() { // 加载设置 Message = Settings.GetValue("Message", "Hello, World!"); return Task.CompletedTask; } // 组件更新 (定时调用) public override Task UpdateAsync() { // 更新组件数据 return Task.CompletedTask; } } ``` ### 2. 创建组件视图 **MyComponentView.axaml:** ```xml