# 创建第一个轻应用 通过这个教程,你将在 15 分钟内创建一个简单但功能完整的轻应用。 ## 学习目标 - ✅ 使用模板创建轻应用项目 - ✅ 实现轻应用入口类 - ✅ 创建一个简单的桌面组件 - ✅ 注册组件到宿主 - ✅ 运行和测试轻应用 ## 前置准备 确保你已经: - ✅ 安装了 .NET 10 SDK - ✅ 安装了轻应用模板(参考 [环境准备](01-环境准备.md)) - ✅ 有一个支持 C# 的 IDE ## 步骤 1: 创建项目 ### 使用模板创建 ```powershell # 创建新轻应用项目 dotnet new lmd-airapp -n HelloWorldAirApp # 进入项目目录 cd HelloWorldAirApp # 还原依赖 dotnet restore ``` ### 项目结构预览 ``` HelloWorldAirApp/ ├── HelloWorldAirApp.csproj # 项目文件 ├── AirApp.cs # 轻应用入口(我们要修改这个) ├── airapp.json # 轻应用清单(我们要修改这个) ├── Components/ │ └── SampleComponent.cs # 示例组件(我们要修改这个) ├── Views/ │ └── SampleComponentView.axaml # 组件视图(我们要修改这个) ├── ViewModels/ │ └── SampleComponentViewModel.cs ├── Assets/ │ └── icon.png # 轻应用图标 └── Settings/ └── AirAppSettingsPage.axaml # 设置页 ``` ## 步骤 2: 配置轻应用清单 编辑 `airapp.json`,修改基本信息: ```json { "Id": "com.example.helloworldplugin", "Name": "Hello World AirApp", "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: 实现轻应用入口 编辑 `AirApp.cs`: ```csharp using LanMountainDesktop.AirAppSdk; using LanMountainDesktop.Shared.Contracts; using HelloWorldAirApp.Components; using HelloWorldAirApp.Views; using HelloWorldAirApp.ViewModels; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace HelloWorldAirApp; /// /// Hello World 轻应用入口 /// public class AirApp : IAirApp { public string Id => "com.example.helloworldplugin"; public string Name => "Hello World AirApp"; public string Version => "1.0.0"; private IAirAppRuntimeContext? _context; /// /// 轻应用初始化 /// public async Task InitializeAsync(IAirAppRuntimeContext context) { _context = context; // 记录日志 context.Logger.LogInformation("Hello World AirApp 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 AirApp is shutting down..." ); // 清理资源(如果有) await Task.CompletedTask; } } ``` ### 代码说明 1. **实现 IAirApp 接口** - 定义轻应用的基本信息和生命周期 2. **InitializeAsync** - 轻应用加载时调用,注册组件和服务 3. **ShutdownAsync** - 轻应用卸载时调用,清理资源 4. **日志记录** - 使用 `context.Logger` 记录日志 ## 步骤 4: 创建组件类 编辑 `Components/SampleComponent.cs`,重命名为 `HelloWorldComponent.cs`: ```csharp using LanMountainDesktop.AirAppSdk.Components; using LanMountainDesktop.Shared.Contracts.Components; using System.ComponentModel; namespace HelloWorldAirApp.Components; /// /// Hello World 桌面组件 /// [Component( Id = "com.example.helloworldplugin.helloworld", Name = "Hello World", Description = "Displays a friendly greeting message", Category = "Demo", Icon = "avares://HelloWorldAirApp/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