# 创建第一个插件 通过这个教程,你将在 15 分钟内创建一个简单但功能完整的插件。 ## 学习目标 - ✅ 使用模板创建插件项目 - ✅ 实现插件入口类 - ✅ 创建一个简单的桌面组件 - ✅ 注册组件到宿主 - ✅ 运行和测试插件 ## 前置准备 确保你已经: - ✅ 安装了 .NET 10 SDK - ✅ 安装了插件模板(参考 [环境准备](01-环境准备.md)) - ✅ 有一个支持 C# 的 IDE ## 步骤 1: 创建项目 ### 使用模板创建 ```powershell # 创建新插件项目 dotnet new lmd-plugin -n HelloWorldPlugin # 进入项目目录 cd HelloWorldPlugin # 还原依赖 dotnet restore ``` ### 项目结构预览 ``` HelloWorldPlugin/ ├── HelloWorldPlugin.csproj # 项目文件 ├── Plugin.cs # 插件入口(我们要修改这个) ├── plugin.json # 插件清单(我们要修改这个) ├── Components/ │ └── SampleComponent.cs # 示例组件(我们要修改这个) ├── Views/ │ └── SampleComponentView.axaml # 组件视图(我们要修改这个) ├── ViewModels/ │ └── SampleComponentViewModel.cs ├── Assets/ │ └── icon.png # 插件图标 └── Settings/ └── PluginSettingsPage.axaml # 设置页 ``` ## 步骤 2: 配置插件清单 编辑 `plugin.json`,修改基本信息: ```json { "Id": "com.example.helloworldplugin", "Name": "Hello World Plugin", "Version": "1.0.0", "Author": "Your Name", "Description": "My first LanMountainDesktop plugin - displays a greeting", "MinHostVersion": "1.0.0", "SdkVersion": "5.0.0", "Dependencies": [], "Permissions": [], "Icon": "Assets/icon.png", "Homepage": "https://github.com/yourusername/helloworldplugin" } ``` ### 字段说明 - **Id**: 插件唯一标识符,建议使用反向域名格式 - **Name**: 用户看到的插件名称 - **Version**: 插件版本号(语义化版本) - **MinHostVersion**: 最低支持的宿主版本 - **SdkVersion**: 使用的 SDK 版本 ## 步骤 3: 实现插件入口 编辑 `Plugin.cs`: ```csharp using LanMountainDesktop.PluginSdk; using LanMountainDesktop.Shared.Contracts; using HelloWorldPlugin.Components; using HelloWorldPlugin.Views; using HelloWorldPlugin.ViewModels; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace HelloWorldPlugin; /// /// Hello World 插件入口 /// public class Plugin : IPlugin { public string Id => "com.example.helloworldplugin"; public string Name => "Hello World Plugin"; public string Version => "1.0.0"; private IPluginContext? _context; /// /// 插件初始化 /// public async Task InitializeAsync(IPluginContext context) { _context = context; // 记录日志 context.Logger.LogInformation("Hello World Plugin is initializing..."); // 获取组件注册表 var componentRegistry = context.Services .GetService(); if (componentRegistry != null) { // 注册 Hello World 组件 componentRegistry.RegisterComponent( componentFactory: () => new HelloWorldComponent(), viewFactory: (component) => new HelloWorldComponentView { DataContext = new HelloWorldComponentViewModel( (HelloWorldComponent)component ) } ); context.Logger.LogInformation( "HelloWorldComponent registered successfully" ); } // 异步操作示例(如果需要) await Task.CompletedTask; } /// /// 插件关闭 /// public async Task ShutdownAsync() { _context?.Logger.LogInformation( "Hello World Plugin is shutting down..." ); // 清理资源(如果有) await Task.CompletedTask; } } ``` ### 代码说明 1. **实现 IPlugin 接口** - 定义插件的基本信息和生命周期 2. **InitializeAsync** - 插件加载时调用,注册组件和服务 3. **ShutdownAsync** - 插件卸载时调用,清理资源 4. **日志记录** - 使用 `context.Logger` 记录日志 ## 步骤 4: 创建组件类 编辑 `Components/SampleComponent.cs`,重命名为 `HelloWorldComponent.cs`: ```csharp using LanMountainDesktop.PluginSdk.Components; using LanMountainDesktop.Shared.Contracts.Components; using System.ComponentModel; namespace HelloWorldPlugin.Components; /// /// Hello World 桌面组件 /// [Component( Id = "com.example.helloworldplugin.helloworld", Name = "Hello World", Description = "Displays a friendly greeting message", Category = "Demo", Icon = "avares://HelloWorldPlugin/Assets/icon.png" )] public class HelloWorldComponent : ComponentBase { public override string Id => "com.example.helloworldplugin.helloworld"; public override string Name => "Hello World"; private string _greeting = "Hello, World!"; private int _clickCount = 0; /// /// 问候语 /// public string Greeting { get => _greeting; set => SetProperty(ref _greeting, value); } /// /// 点击次数 /// public int ClickCount { get => _clickCount; set => SetProperty(ref _clickCount, value); } /// /// 组件初始化 /// public override Task InitializeAsync() { // 从设置加载问候语 Greeting = Settings.GetValue("Greeting", "Hello, World!"); ClickCount = Settings.GetValue("ClickCount", 0); Logger.LogInformation("HelloWorldComponent initialized"); return Task.CompletedTask; } /// /// 组件更新(定时调用) /// public override Task UpdateAsync() { // 这里可以更新组件数据 // 例如:从 API 获取数据、更新时间等 return Task.CompletedTask; } /// /// 增加点击次数 /// public void IncrementClickCount() { ClickCount++; Settings.SetValue("ClickCount", ClickCount); } } ``` ### 组件说明 - **ComponentBase** - 所有组件的基类,提供基础功能 - **属性通知** - 使用 `SetProperty` 自动触发 UI 更新 - **设置持久化** - 使用 `Settings` 保存和读取配置 - **InitializeAsync** - 组件创建时调用 - **UpdateAsync** - 定时调用(默认 1 秒),用于更新数据 ## 步骤 5: 创建组件视图 编辑 `Views/SampleComponentView.axaml`,重命名为 `HelloWorldComponentView.axaml`: ```xml