Skip to content

Quick Start

Ling edited this page Jul 7, 2026 · 4 revisions

快速开始

本页从零创建一个最小 C# 插件。

1. 创建项目

dotnet new classlib -n HelloPlugin --framework net8.0

编辑 HelloPlugin.csproj,改为 Windows/WPF 目标框架并引用 SDK:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <EnableWindowsTargeting>true</EnableWindowsTargeting>
    <OutputType>Library</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PlainCraftLauncher.Plugin.Abstractions" Version="1.1.0" ExcludeAssets="runtime" />
  </ItemGroup>
</Project>

ExcludeAssets="runtime" 很重要。插件包不应携带自己的 PCL.Plugin.Abstractions.dll,否则宿主和插件可能看到两份不同的接口程序集。

2. 编写入口类

using System.Threading;
using System.Threading.Tasks;
using PCL.Plugin.Abstractions;

namespace Example.HelloPlugin;

[Plugin(
    id: "com.example.hello",
    name: "Hello Plugin",
    version: "1.0.0.0",
    Author = "Example",
    Description = "A minimal PCL plugin.",
    MinApiVersion = "1.1.0.0",
    Capabilities = PluginCapabilities.None,
    LoadTiming = PluginLoadTiming.WindowCreated)]
public sealed class HelloPlugin : PclPluginBase
{
    public override Task LoadAsync(IPluginContext context, CancellationToken cancellationToken = default)
    {
        context.Host.Core.Hint("Hello from plugin", PluginHintType.Success);
        context.Host.Core.GetLogger("hello").Info("Hello Plugin loaded.");
        return Task.CompletedTask;
    }
}

3. 添加 plugin.json

{
  "id": "com.example.hello",
  "name": "Hello Plugin",
  "version": "1.0.0.0",
  "author": "Example",
  "description": "A minimal PCL plugin.",
  "entry": "HelloPlugin.dll",
  "type": "dotnet",
  "minApiVersion": "1.1.0.0",
  "capabilities": []
}

4. 构建

dotnet build .\HelloPlugin.csproj --configuration Release --property:Platform=AnyCPU

5. 安装

把插件包放到 PCL 插件目录下:

PCL/Plugins/com.example.hello/
  plugin.json
  HelloPlugin.dll
  其他依赖 DLL

重启 PCL 后,宿主会扫描插件目录并加载插件。

下一步

Clone this wiki locally