Files
serein-flow/WorkBench/App.xaml.cs

195 lines
6.1 KiB
C#
Raw Normal View History

2024-08-06 16:09:46 +08:00
using Newtonsoft.Json;
using Serein.Library;
2024-12-10 23:58:49 +08:00
using System.Diagnostics;
2024-11-02 16:48:40 +08:00
using System.IO;
2024-08-05 10:11:58 +08:00
using System.Windows;
using System.Windows.Threading;
2024-08-05 10:11:58 +08:00
namespace Serein.Workbench
2024-08-05 10:11:58 +08:00
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
void LoadLocalProject()
{
2024-12-12 20:33:08 +08:00
#if DEBUG
2024-12-12 21:09:29 +08:00
if (1 == 1)
{
2024-12-12 20:33:08 +08:00
// 这里是我自己的测试代码,你可以删除
string filePath;
2024-11-02 16:48:40 +08:00
filePath = @"F:\临时\project\linux\project.dnf";
2024-10-28 21:52:45 +08:00
filePath = @"F:\临时\project\linux\http\project.dnf";
2024-11-02 16:48:40 +08:00
filePath = @"F:\临时\project\yolo flow\project.dnf";
filePath = @"F:\临时\project\data\project.dnf";
filePath = @"C:\Users\Az\source\repos\CLBanyunqiState\CLBanyunqiState\bin\Release\net8.0\project.dnf";
filePath = @"C:\Users\Az\source\repos\CLBanyunqiState\CLBanyunqiState\bin\Release\net8.0\PLCproject.dnf";
string content = System.IO.File.ReadAllText(filePath); // 读取整个文件内容
App.FlowProjectData = JsonConvert.DeserializeObject<SereinProjectData>(content);
App.FileDataPath = System.IO.Path.GetDirectoryName(filePath)!; // filePath;//
2024-11-02 16:48:40 +08:00
var dir = Path.GetDirectoryName(filePath);
//System.IO.Directory.SetCurrentDirectory(dir);
}
#endif
}
public static SereinProjectData? FlowProjectData { get; set; }
public static string FileDataPath { get; set; } = "";
public App()
{
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
2024-08-05 10:11:58 +08:00
// 强制关闭所有窗口
foreach (Window window in Windows)
{
window.Close();
}
}
private void Application_Startup(object sender, StartupEventArgs e)
{
Application.Current.Dispatcher.Invoke(() => { });
// 检查是否传入了参数
if (e.Args.Length == 1)
{
// 获取文件路径
string filePath = e.Args[0];
// 检查文件是否存在
if (!System.IO.File.Exists(filePath))
{
MessageBox.Show($"文件未找到:{filePath}");
Shutdown(); // 关闭应用程序
return;
}
2024-08-05 10:11:58 +08:00
try
{
// 读取文件内容
string content = System.IO.File.ReadAllText(filePath); // 读取整个文件内容
FlowProjectData = JsonConvert.DeserializeObject<SereinProjectData>(content);
FileDataPath = System.IO.Path.GetDirectoryName(filePath) ?? "";
}
catch (Exception ex)
{
MessageBox.Show($"读取文件时发生错误:{ex.Message}");
Shutdown(); // 关闭应用程序
}
2024-12-10 23:58:49 +08:00
}
this.LoadLocalProject();
}
}
#if DEBUG && false
public class TestObject
2024-08-05 10:11:58 +08:00
{
public NestedObject Data { get; set; }
public class NestedObject
{
public int Code { get; set; }
public int Code2 { get; set; }
public string Tips { get; set; }
}
public string ToUpper(string input)
{
return input.ToUpper();
}
}
//测试 操作表达式,条件表达式
private void TestExp()
{
2024-08-05 10:11:58 +08:00
#region
string expression = "";
var testObj = new TestObject
{
Data = new TestObject.NestedObject
{
Code = 15,
Code2 = 20,
Tips = "测试数据"
}
};
#endregion
#region
// 获取对象成员
var result = SerinExpressionEvaluator.Evaluate("get .Data.Code", testObj);
Debug.WriteLine(result); // 15
// 设置对象成员
SerinExpressionEvaluator.Evaluate("set .Data.Code = 20", testObj);
Debug.WriteLine(testObj.Data.Code); // 20
SerinExpressionEvaluator.Evaluate("set .Data.Tips = 123", testObj);
// 调用对象方法
2024-12-10 23:58:49 +08:00
result = SerinExpressionEvaluator.Evaluate($"call .ToUpper({SerinExpressionEvaluator.Evaluate("get .Data.Tips", testObj)})", testObj);
2024-08-05 10:11:58 +08:00
Debug.WriteLine(result); // HELLO
expression = "@number (@+1)/100";
result = SerinExpressionEvaluator.Evaluate(expression, 2);
Debug.WriteLine($"{expression} -> {result}"); // HELLO
#endregion
#region
expression = ".Data.Code == 15";
var pass = SerinConditionParser.To(testObj, expression);
Debug.WriteLine($"{expression} -> " + pass);
expression = ".Data.Code<int>[@*2] == 31";
//expression = ".Data.Tips<string> contains 数据";
pass = SerinConditionParser.To(testObj, expression);
Debug.WriteLine($"{expression} -> " + pass);
expression = ".Data.Code<int> < 20";
pass = SerinConditionParser.To(testObj, expression);
Debug.WriteLine($"{expression} -> " + pass);
int i = 43;
expression = "in 11-22";
pass = SerinConditionParser.To(i, expression);
Debug.WriteLine($"{i} {expression} -> " + pass);
expression = "== 43";
pass = SerinConditionParser.To(i, expression);
Debug.WriteLine($"{i} {expression} -> " + pass);
string str = "MY NAME IS COOOOL";
expression = "c NAME";
pass = SerinConditionParser.To(str, expression);
Debug.WriteLine($"{str} {expression} -> " + pass);
#endregion
}
2024-09-25 22:20:23 +08:00
#endif
2024-08-05 10:11:58 +08:00
}