using Avalonia_Services.Services; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Avalonia_PC.Views { public partial class MainWindow { // 路由表:key = 接口路径(忽略大小写),value = 处理方法 // 新增接口:在此方法中添加一行 _routes["api/xxx"] = ctx => ... private Dictionary>> _routes = []; // 服务容器,通过构造函数注入,路由注册时按需解析服务 private IServiceProvider _services = null!; private void RegisterRoutes() { var weather = _services.GetRequiredService(); // 新增服务示例:var myService = _services.GetRequiredService(); _routes = new Dictionary>>(StringComparer.OrdinalIgnoreCase) { ["api/getUser"] = _ => GetUserFromDatabaseAsync(), ["api/processData"] = ctx => ProcessDataAsync(ExtractInput(ctx)), ["api/wData"] = _ => Task.FromResult(weather.GetWeatherForecasts()), }; } /// /// 示例:模拟读取用户数据。 /// private static async Task GetUserFromDatabaseAsync() { await Task.Delay(100); return new { id = 1, name = "张三", email = "zhangsan@example.com" }; } /// /// 示例:模拟处理输入数据。 /// private static async Task ProcessDataAsync(string? input) { await Task.Delay(200); return $"Processed: {input?.ToUpperInvariant()}"; } } }