重构中day1,写了很多,不知道怎么说清楚

This commit is contained in:
fengjiayi
2025-03-18 21:01:15 +08:00
parent 87402ec7ea
commit 2168c5ec66
39 changed files with 2809 additions and 10 deletions

View File

@@ -1,18 +1,109 @@
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.NodeFlow.Env;
using Serein.Workbench.Api;
using Serein.Workbench.Services;
using Serein.Workbench.ViewModels;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Threading;
namespace Serein.Workbench
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// 注册ViewModel
/// </summary>
/// <param name="collection"></param>
public static void AddViewModelServices(this IServiceCollection collection)
{
collection.AddSingleton<Locator>(); // 主窗体
collection.AddSingleton<MainViewModel>();
collection.AddSingleton<MainMenuBarViewModel>();
collection.AddSingleton<FlowWorkbenchViewModel>();
collection.AddSingleton<BaseNodesViewModel>();
collection.AddSingleton<FlowLibrarysViewModel>();
collection.AddSingleton<FlowEditViewModel>();
collection.AddTransient<FlowCanvasViewModel>(); // 依赖信息
}
public static void AddWorkbenchServices(this IServiceCollection collection)
{
collection.AddSingleton<IFlowEEForwardingService, FlowEEForwardingService>(); // 流程事件管理
collection.AddSingleton<IWorkbenchEventService, WorkbenchEventService>(); // 流程事件管理
collection.AddSingleton<INodeOperationService, NodeOperationService>(); // 节点操作管理
// collection.AddSingleton<IKeyEventService, KeyEventService>(); // 按键事件管理
//collection.AddSingleton<FlowNodeControlService>(); // 流程节点控件管理
}
/// <summary>
/// 注册流程接口相关实例
/// </summary>
/// <param name="collection"></param>
public static void AddFlowServices(this IServiceCollection collection)
{
#region
Func<SynchronizationContext> getSyncContext = null;
Dispatcher.CurrentDispatcher.Invoke(() =>
{
var uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
if (uiContext is not null)
{
getSyncContext = () => uiContext;
}
});
UIContextOperation? uIContextOperation = null;
uIContextOperation = new UIContextOperation(getSyncContext); // 封装一个调用UI线程的工具类
var flowEnvironmentDecorator = new FlowEnvironmentDecorator();
flowEnvironmentDecorator.SetUIContextOperation(uIContextOperation);
collection.AddSingleton<UIContextOperation>(uIContextOperation); // 注册UI线程操作上下文
collection.AddSingleton<IFlowEnvironment>(flowEnvironmentDecorator); // 注册运行环境
collection.AddSingleton<IFlowEnvironmentEvent>(flowEnvironmentDecorator); // 注册运行环境事件
#endregion
}
}
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static IServiceProvider? ServiceProvider;
public static T GetService<T>() where T : class
{
return ServiceProvider?.GetService<T>() ?? throw new NullReferenceException();
}
public App()
{
var collection = new ServiceCollection();
collection.AddWorkbenchServices();
collection.AddFlowServices();
collection.AddViewModelServices();
var services = collection.BuildServiceProvider(); // 绑定并返回获取实例的服务接口
App.ServiceProvider = services;
_ = Task.Run(async () =>
{
await Task.Delay(500);
await this.LoadLocalProjectAsync();
});
}
private async Task LoadLocalProjectAsync()
{
@@ -30,10 +121,14 @@ namespace Serein.Workbench
App.FlowProjectData = JsonConvert.DeserializeObject<SereinProjectData>(content);
App.FileDataPath = System.IO.Path.GetDirectoryName(filePath)!; // filePath;//
var dir = Path.GetDirectoryName(filePath);
App.GetService<IFlowEnvironment>().LoadProject(new FlowEnvInfo { Project = App.FlowProjectData },App.FileDataPath);
}
#endif
}
public static SereinProjectData? FlowProjectData { get; set; }
public static string FileDataPath { get; set; } = "";
@@ -66,7 +161,7 @@ namespace Serein.Workbench
}
}
await this.LoadLocalProjectAsync();
}