Skip to content

Commit a25194e

Browse files
committed
up 调整说明与示例
1 parent 7d55bb9 commit a25194e

13 files changed

+225
-136
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@
6969
前往[API版本说明](./api.md)
7070

7171
## 项目说明
72-
- ColorDesktop.Api 插件接口
73-
- ColorDesktop.Debug 调试用
72+
- ColorDesktop.Api 组件接口
73+
- ColorDesktop.Debug 调试用,用于预览组件显示实例
7474
- ColorDesktop.Launcher 程序本体
75-
- ColorDesktop.PluginList 插件源生成器
75+
- ColorDesktop.PluginList 组件json源生成器,发布组件的时候用
76+
- ColorDesktop.WebApi 浏览器组件接口
7677

7778
## 依赖/引用的项目
7879
[AvaloniaUI](https://github.com/AvaloniaUI/Avalonia) 跨平台UI框架
@@ -86,7 +87,7 @@
8687
## 组件依赖/引用的项目
8788
[LibreHardwareMonitor](https://github.com/LibreHardwareMonitor/LibreHardwareMonitor) 传感器读取
8889
[lunar-csharp](https://github.com/6tail/lunar-csharp) 农历节气天干地支等计算
89-
[Live2DCSharpSDK](https://github.com/Coloryr/Live2DCSharpSDK) Live2D显示
90+
~~[Live2DCSharpSDK](https://github.com/Coloryr/Live2DCSharpSDK) Live2D显示~~
9091
[MinecraftSkinRender](https://github.com/Coloryr/MinecraftSkinRender) Minecraft皮肤渲染器
9192

9293
## 开源协议

dev.md

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 组件开发指南
22

3+
这个是C#组件开发指南,[前往浏览器组件](./web.md)
4+
35
在开发组件之前,你需要有以下知识
46
- [C# 12](https://learn.microsoft.com/zh-cn/dotnet/csharp/)
57
- [axaml](https://docs.avaloniaui.net/docs/basics/user-interface/introduction-to-xaml)
@@ -24,6 +26,37 @@ dotnet build
2426

2527
此时,会构建到`./src/build_out/Debug/net8.0`,你可以在这里面找到`ColorDesktop.Launcher.exe`
2628

29+
然后是组件引用`ColorDesktop.Api`,有两种方式实现
30+
31+
- 使用源码的方式引用API
32+
1. 打开`ColorDesktop.sln`,在里面新建你的组件项目
33+
2. 修改`ColorDesktop.Debug`,并引用你的组件项目,
34+
3. 修改`ColorDesktop.Debug``Program`函数`BuildAvaloniaApp`,在里面添加你的组件
35+
```c#
36+
new AnalogClockPlugin.AnalogClockPlugin().LoadLang(LanguageType.zh_cn);
37+
```
38+
你的组件类可能,没有,你可以后面再回来加,如果不加上会导致语言加载错误
39+
40+
- 使用Nuget包方式引用API
41+
1. 创建一个C# 类库工程
42+
2. 安装[nuget包`ColorDesktop.Api`](https://www.nuget.org/packages/ColorDesktop.Api)
43+
3. 创建一个Avalonia项目,同时引用你的类库工程
44+
4. 删除里面的所有`PackageReference`引用`Avalonia`项,例如
45+
```xml
46+
<!-- 这些全部删掉 -->
47+
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
48+
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
49+
<!-- 等... -->
50+
```
51+
5. 修改控制台应用的`Program`函数`BuildAvaloniaApp`,在里面添加你的组件
52+
```c#
53+
new AnalogClockPlugin.AnalogClockPlugin().LoadLang(LanguageType.zh_cn);
54+
```
55+
你的组件类可能,没有,你可以后面再回来加,如果不加上会导致语言加载错误
56+
57+
完成以上操作后,就可以开始开发组件了,同时若VS装上了`Avalonia预览插件`,也可以预览你的UI了
58+
**注意,如果使用nuget方式,则在预览中不会使用ColorDesktop的样式,但是组件加载后会使用**
59+
2760
## 组件与显示实例概念
2861

2962
`组件`是软件的附属,组件里面有用于操作与显示的内容
@@ -82,6 +115,7 @@ dotnet build
82115
//组件作者
83116
"Auther": "Coloryr",
84117
//组件程序集,就是dll的名字,在这里可以添加你的依赖库等
118+
//这个列表里面是最先加载的,若你的运行库很多,可以不写,会后面自动加载
85119
"Dlls": [
86120
"ColorDesktop.AnalogClockPlugin"
87121
],
@@ -113,7 +147,7 @@ dotnet build
113147
"ApiVersion": "4"
114148
}
115149
```
116-
然后设置编译后复制
150+
然后设置编译后设置`编译后复制`,或者在工程内加入这段
117151
```xml
118152
<ItemGroup>
119153
<None Update="plugin.json">
@@ -122,19 +156,18 @@ dotnet build
122156
</ItemGroup>
123157
```
124158

125-
编写组件主入口类,需要实现接口[ColorDesktop.Api.IPlugin](./src/ColorDesktop.Api/IPlugin.cs)
159+
然后编写组件主入口类,需要实现接口[ColorDesktop.Api.IPlugin](./src/ColorDesktop.Api/IPlugin.cs)
160+
然后编写显示实例,需要实现接口[ColorDesktop.Api.IInstance](./src/ColorDesktop.Api/IInstance.cs)
126161

127-
编写显示实例,需要实现接口[ColorDesktop.Api.IInstance](./src/ColorDesktop.Api/IInstance.cs)
128-
129-
组件生命周期
162+
## 组件生命周期
130163
![](./pic/pic8.png)
131164
组件相关的生命周期有四个函数
132165
- Init 只在组件初始化时调用,整个生命周期只会调用一次
133166
- Enable 组件启用时调用,会多次调用,在[组件管理](./README.md#组件管理)界面下的`启用组件`就会调用这个
134167
- Disable 组件卸载时调用,会多次调用,在[组件管理](./README.md#组件管理)界面下的`禁用组件`就会调用这个
135168
- Stop 组件关闭时调用,整个生命周期只会调用一次,调用后组件会被卸载
136169

137-
显示实例生命周期
170+
## 显示实例生命周期
138171
![](./pic/pic9.png)
139172
显示实例相关的生命周期有五个函数
140173
- CreateView 只会调用一次,需要返回一个Avalonia的Control,及显示内容
@@ -161,11 +194,6 @@ return new InstanceDataObj()
161194

162195
更多具体内容可以参考[示例插件](./src/Plugins/ColorDesktop.ClockPlugin/ClockPlugin.cs)[示例显示实例](./src/Plugins/ColorDesktop.ClockPlugin/ClockControl.axaml.cs)
163196

164-
## 预览
165-
166-
如果你使用了VS2022,并安装了Avalonia插件,但是无法预览页面
167-
此时你需要将[ColorDesktop.Debug](./src/ColorDesktop.Debug/ColorDesktop.Debug.csproj)引入到你的解决方案中,添加你的组件为依赖项目,然后编译,选择启动项为`ColorDesktop.Debug`即可
168-
169197
## 组件公共API
170198

171199
在类`ColorDesktop.Api.LauncherApi`里面可以获取一些用于操作的API
@@ -209,7 +237,8 @@ public static void CallEvent(BaseEvent pluginEvent);
209237

210238
## CoreLib
211239
这个是`ColorDesktop.CoreLib`组件
212-
主要是用于将重复代码抽出来弄成一个组件方便其他组件调用
240+
主要是用于将重复代码抽出来弄成一个组件方便其他组件调用
241+
~~这里是Coloryr组件用的CoreLib,你可以自己去写一个~~
213242

214243
使用前需要将其以共享的方式作为依赖
215244
```json

src/ColorDesktop.WebApi/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ColorDesktop WebPlugin Api

src/ColorDesktop.WebApi/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "colordesktop-webapi",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"scripts": {
55
"build": "tsc --build",
66
"clean": "tsc --build --clean",
77
"link": "link"
88
},
9+
"keywords": ["colordesktop", "api", "coloryr"],
910
"main": "dist/index.js",
1011
"types": "dist/index.d.ts",
1112
"author": "coloryr",

src/Demo/PluginDemo/DemoControl.axaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
xmlns="https://github.com/avaloniaui"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:lang="clr-namespace:ColorDesktop.Api;assembly=ColorDesktop.Api"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
78
xmlns:model="clr-namespace:PluginDemo;assembly=PluginDemo"
8-
d:DesignHeight="450"
9-
d:DesignWidth="800"
109
x:DataType="model:DemoModel"
1110
mc:Ignorable="d">
1211
<StackPanel
1312
HorizontalAlignment="Center"
1413
VerticalAlignment="Center"
1514
Background="White">
1615
<TextBlock Text="{Binding Text}" />
17-
<Button Command="{Binding Click}" Content="Click Me!" />
16+
<Button Command="{Binding Click}" Content="{lang:Localize DemoPlugin.Text1}" />
1817
</StackPanel>
1918
</UserControl>

src/Demo/PluginDemo/DemoInstanceSettingControl.axaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
xmlns="https://github.com/avaloniaui"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:lang="clr-namespace:ColorDesktop.Api;assembly=ColorDesktop.Api"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
78
xmlns:model="clr-namespace:PluginDemo;assembly=PluginDemo"
8-
d:DesignHeight="450"
9-
d:DesignWidth="800"
109
x:DataType="model:DemoInstanceSettingModel"
1110
mc:Ignorable="d">
12-
<TextBox Text="{Binding Text}" />
11+
<StackPanel>
12+
<TextBlock Text="{lang:Localize DemoPlugin.Text2}" />
13+
<TextBox Text="{Binding Text}" />
14+
</StackPanel>
1315
</UserControl>

src/Demo/PluginDemo/DemoPlugin.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Avalonia.Controls;
1+
using System.Reflection;
2+
using Avalonia.Controls;
23
using ColorDesktop.Api;
34
using ColorDesktop.Api.Objs;
45

@@ -132,7 +133,19 @@ public void Init(string local, string instancelocal)
132133

133134
public void LoadLang(LanguageType type)
134135
{
135-
136+
var assm = Assembly.GetExecutingAssembly();
137+
if (assm == null)
138+
{
139+
return;
140+
}
141+
string name = type switch
142+
{
143+
LanguageType.en_us => "PluginDemo.Resource.Lang.en-us.json",
144+
_ => "PluginDemo.Resource.Lang.zh-cn.json"
145+
};
146+
using var item = assm.GetManifestResourceStream(name)!;
147+
using var reader = new StreamReader(item);
148+
LangApi.AddLangs(reader.ReadToEnd());
136149
}
137150

138151
public IInstance MakeInstances(InstanceDataObj obj)

src/Demo/PluginDemo/DemoSettingControl.axaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
xmlns="https://github.com/avaloniaui"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:lang="clr-namespace:ColorDesktop.Api;assembly=ColorDesktop.Api"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
78
xmlns:model="clr-namespace:PluginDemo;assembly=PluginDemo"
8-
d:DesignHeight="450"
9-
d:DesignWidth="800"
109
x:DataType="model:DemoSettingModel"
1110
mc:Ignorable="d">
12-
<TextBox Text="{Binding Text}" />
11+
<StackPanel>
12+
<TextBlock Text="{lang:Localize DemoPlugin.Text2}" />
13+
<TextBox Text="{Binding Text}" />
14+
</StackPanel>
1315
</UserControl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"DemoPlugin.Name": "模拟时钟",
3+
"DemoPlugin.Text1": "点击我",
4+
"DemoPlugin.Text2": "设置显示文本"
5+
}

0 commit comments

Comments
 (0)