diff --git a/PLCSharpWpf/App.xaml.cs b/PLCSharpWpf/App.xaml.cs index 9aa97b6..653b0b7 100644 --- a/PLCSharpWpf/App.xaml.cs +++ b/PLCSharpWpf/App.xaml.cs @@ -38,12 +38,12 @@ namespace PLCSharp ForegroundWindow(); Environment.Exit(0); } + LoadConfig(); //数据库 _ = containerRegistry.RegisterSingleton(); _ = Container.Resolve(); RegisterEvents(); // 捕获全局异常 - LoadConfig(); //Module中的初始化晚于这里,所以不能在Module中注册全局使用的实例,比如NavigateModel diff --git a/PLCSharpWpf/Core/Tools/WpfTool.cs b/PLCSharpWpf/Core/Tools/WpfTool.cs index 83ea10f..53d650c 100644 --- a/PLCSharpWpf/Core/Tools/WpfTool.cs +++ b/PLCSharpWpf/Core/Tools/WpfTool.cs @@ -30,7 +30,7 @@ namespace PLCSharp.Core.Tools PointsQueue.TryDequeue(out PointF point); Points.Add(point); } - await Application.Current?.Dispatcher.BeginInvoke(() => + await Application.Current.Dispatcher.InvokeAsync(() => { WriteableBitmap writeableBitmap = _imgSource; int width = (int)writeableBitmap.Width; @@ -89,7 +89,7 @@ namespace PLCSharp.Core.Tools public static System.Windows.Media.Color GetRandColor(int start = 0, int end = 256) { // 创建一个Random对象 - Random random = new Random(); + Random random = new(); // 生成一个0到255之间的随机整数作为红色分量 byte red = Convert.ToByte(random.Next(start, end)); diff --git a/PLCSharpWpf/Core/Tools/XPoint.cs b/PLCSharpWpf/Core/Tools/XPoint.cs index abf9172..dce0262 100644 --- a/PLCSharpWpf/Core/Tools/XPoint.cs +++ b/PLCSharpWpf/Core/Tools/XPoint.cs @@ -1,4 +1,4 @@ - +#nullable enable using Prism.Mvvm; @@ -73,7 +73,7 @@ namespace PLCSharp.Core.Tools return to; } - public bool Equals(XPoint other) + public bool Equals(XPoint? other) { if (other is null) return false; // 避免浮点数精度问题,通常使用一个极小值 Epsilon 进行比较 diff --git a/PLCSharpWpf/GlobalSuppressions.cs b/PLCSharpWpf/GlobalSuppressions.cs index 28c7d82..b779936 100644 --- a/PLCSharpWpf/GlobalSuppressions.cs +++ b/PLCSharpWpf/GlobalSuppressions.cs @@ -7,7 +7,7 @@ using System.Diagnostics.CodeAnalysis; [assembly: SuppressMessage("Style", "IDE1006:命名样式")] [assembly: SuppressMessage("Style", "IDE0290:使用主构造函数")] -[assembly: SuppressMessage("Performance", "SYSLIB1045:转换为“GeneratedRegexAttribute”。")] -[assembly: SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<挂起>", Scope = "member", Target = "~M:PLCSharp.VVMs.Workflows.Script.CustomTextEditor.OnDataContextChanged(System.Object,System.Windows.DependencyPropertyChangedEventArgs)")] -[assembly: SuppressMessage("Usage", "VSTHRD001:Avoid legacy thread switching APIs")] [assembly: SuppressMessage("Style", "IDE0305:简化集合初始化")] +[assembly: SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks")] +[assembly: SuppressMessage("Usage", "VSTHRD001:Avoid legacy thread switching APIs")] + \ No newline at end of file diff --git a/PLCSharpWpf/PLCSharp.csproj b/PLCSharpWpf/PLCSharp.csproj index 839e15a..67773eb 100644 --- a/PLCSharpWpf/PLCSharp.csproj +++ b/PLCSharpWpf/PLCSharp.csproj @@ -21,19 +21,16 @@ - + - + - + - - - Never diff --git a/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuClient.cs b/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuClient.cs index e526c95..1577fa1 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuClient.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuClient.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using System.Net; using System.Threading; @@ -29,7 +29,7 @@ namespace PLCSharp.VVMs.Connects.ModbusRtu /// 打开串口 - public bool Open() + public override void Open() { try { @@ -42,13 +42,13 @@ namespace PLCSharp.VVMs.Connects.ModbusRtu _port.Open(); Connected = true; Log($"串口 {IP_SerialPort} 已打开"); - return true; + } catch (Exception ex) { Log($"打开串口失败: {ex.Message}"); Connected = false; - return false; + } } diff --git a/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs index 47f24f9..22ccfdd 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs @@ -42,7 +42,7 @@ namespace PLCSharp.VVMs.Connects.ModbusRtu /// /// 标题 /// - public string Title => "Modbus RTU 客户端"; + public static string Title => "Modbus RTU 客户端"; /// /// Request关闭 @@ -180,7 +180,7 @@ namespace PLCSharp.VVMs.Connects.ModbusRtu { if (Client.LogQueue.TryDequeue(out string log)) { - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs index a2d940c..bacb372 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs @@ -31,7 +31,6 @@ namespace PLCSharp.VVMs.Connects.ModbusTcp private readonly ConnectsModel _ConnectsModel; private string _Title = "Modbus TCP 客户端"; - private bool _hasSubscribedCollection; /// /// 标题 @@ -97,7 +96,7 @@ namespace PLCSharp.VVMs.Connects.ModbusTcp { if (Client.LogQueue.TryDequeue(out string log)) { - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs index 1ff0a86..e880fd2 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs @@ -200,7 +200,7 @@ namespace PLCSharp.VVMs.Connects.ModbusTcp { if (Server.LogQueue.TryDequeue(out string log)) { - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs index 18f024e..fd1e1ee 100644 --- a/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs @@ -102,7 +102,7 @@ namespace PLCSharp.VVMs.Connects.SerialPort { if (Client.LogQueue.TryDequeue(out string log)) { - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs index 924e6a7..aa77b75 100644 --- a/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs @@ -102,7 +102,7 @@ namespace PLCSharp.VVMs.Connects.Socket { if ((Client as SocketClient).LogQueue.TryDequeue(out string log)) - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs index f996fac..bc0efed 100644 --- a/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs @@ -107,7 +107,7 @@ namespace PLCSharp.VVMs.Connects.Socket if (!(Server as SocketServer).LogQueue.IsEmpty) { if ((Server as SocketServer).LogQueue.TryDequeue(out string log)) - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Homepage/CustomControl.cs b/PLCSharpWpf/VVMs/Homepage/CustomControl.cs index 85eafa7..9ab1f61 100644 --- a/PLCSharpWpf/VVMs/Homepage/CustomControl.cs +++ b/PLCSharpWpf/VVMs/Homepage/CustomControl.cs @@ -214,7 +214,7 @@ namespace PLCSharp.VVMs.Homepage } private static void Dispatch(Action action) { - _= Application.Current.Dispatcher.BeginInvoke(action); + _= Application.Current.Dispatcher.InvokeAsync(action); } /// diff --git a/PLCSharpWpf/VVMs/Homepage/TableControl/RangeTableViewModel.cs b/PLCSharpWpf/VVMs/Homepage/TableControl/RangeTableViewModel.cs index f98fb7b..dabf716 100644 --- a/PLCSharpWpf/VVMs/Homepage/TableControl/RangeTableViewModel.cs +++ b/PLCSharpWpf/VVMs/Homepage/TableControl/RangeTableViewModel.cs @@ -1,3 +1,4 @@ +#nullable enable using Prism.Mvvm; using System.Collections.ObjectModel; using System.Linq; diff --git a/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs b/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs index 134e0cd..9390787 100644 --- a/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs +++ b/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs @@ -1,3 +1,4 @@ +#nullable enable using Prism.Mvvm; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -23,7 +24,7 @@ namespace PLCSharp.VVMs.Robots /// public Guid RobotID { get; set; } - private string _Name; + private string _Name = ""; /// /// 名称 /// @@ -33,7 +34,7 @@ namespace PLCSharp.VVMs.Robots set { SetProperty(ref _Name, value); } } - private string _StartName; + private string _StartName = ""; /// /// 首点名(原点 (0,0)) /// @@ -43,7 +44,7 @@ namespace PLCSharp.VVMs.Robots set { SetProperty(ref _StartName, value); } } - private string _XEndName; + private string _XEndName = ""; /// /// X方向尾点名((XCount-1, 0)) /// @@ -53,7 +54,7 @@ namespace PLCSharp.VVMs.Robots set { SetProperty(ref _XEndName, value); } } - private string _YEndName; + private string _YEndName = ""; /// /// Y方向尾点名((0, YCount-1)) /// diff --git a/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs index a725312..913b120 100644 --- a/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs @@ -46,7 +46,7 @@ namespace PLCSharp.VVMs.Vision.Camera { SetProperty(ref _ShowMat, value); if (value == null) return; - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { if (_ImgSrc != null && _ShowMat.Width == _ImgSrc.PixelWidth diff --git a/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs b/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs index 180dd2b..fe6909c 100644 --- a/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs +++ b/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs @@ -1,6 +1,6 @@ using Newtonsoft.Json; using Prism.Mvvm; - + namespace PLCSharp.VVMs.Vision { /// @@ -8,7 +8,7 @@ namespace PLCSharp.VVMs.Vision /// public class LocalVariableItem : BindableBase { - private string _Name; + private string _Name = ""; /// /// 变量名称 /// @@ -18,7 +18,7 @@ namespace PLCSharp.VVMs.Vision set { SetProperty(ref _Name, value); } } - private string _VarType; + private string _VarType = ""; /// /// 变量类型:Point / Line / Circle / Rect / Double /// @@ -28,7 +28,7 @@ namespace PLCSharp.VVMs.Vision set { SetProperty(ref _VarType, value); } } - private string _StrValue; + private string _StrValue = ""; [JsonIgnore] public string StrValue { @@ -36,11 +36,11 @@ namespace PLCSharp.VVMs.Vision set { SetProperty(ref _StrValue, value); } } - private object? _RawValue; + private object _RawValue = ""; /// /// 原始值 /// - public object? RawValue + public object RawValue { get { return _RawValue; } set @@ -58,6 +58,7 @@ namespace PLCSharp.VVMs.Vision } else { + if (value == null) break ; r = JsonConvert.DeserializeObject(value.ToString()); } SetProperty(ref _RawValue, r); @@ -72,6 +73,7 @@ namespace PLCSharp.VVMs.Vision } else { + if (value == null) break; r = JsonConvert.DeserializeObject(value.ToString()); } SetProperty(ref _RawValue, r); @@ -86,6 +88,7 @@ namespace PLCSharp.VVMs.Vision } else { + if (value == null) break; r = JsonConvert.DeserializeObject(value.ToString()); } SetProperty(ref _RawValue, r); @@ -100,6 +103,7 @@ namespace PLCSharp.VVMs.Vision } else { + if (value == null) break; r = JsonConvert.DeserializeObject(value.ToString()); } @@ -118,6 +122,7 @@ namespace PLCSharp.VVMs.Vision } else { + if (value == null) break; r = JsonConvert.DeserializeObject(value.ToString()); } @@ -133,9 +138,9 @@ namespace PLCSharp.VVMs.Vision catch (Exception) { - + } - + } } @@ -172,25 +177,25 @@ namespace PLCSharp.VVMs.Vision return $"位置(X:{pt.X:F4}, Y:{pt.Y:F4}), 角度:({pt.Angle:F4})"; else return val?.ToString() ?? ""; - + case "Line": if (val is Line line) return $"(X:{line.From.X:F4}, Y:{line.From.Y:F4}) -> (X:{line.To.X:F4}, Y:{line.To.Y:F4})"; else return val?.ToString() ?? ""; - + case "Circle": if (val is Circle c) return $"中心(X:{c.Center.X:F4} Y:{c.Center.Y:F4}) 半径=({c.Radius:F4})"; else return val?.ToString() ?? ""; - + case "Rect": if (val is Rect r) return $"中心:(X:{r.Center.X:F4}, Y:{r.Center.Y:F4}) 宽高:({r.Width:F4} × {r.Height:F4}) 角度:({r.Center.Angle:F4})"; else return val?.ToString() ?? ""; - + case "Double": return ((double)val).ToString("F4"); diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs index c0ae3a2..b18a4aa 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs @@ -285,7 +285,7 @@ namespace PLCSharp.VVMs.Vision { SetProperty(ref _ShowMat, value); if (value == null) return; - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { try { diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ClearDrawHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ClearDrawHandler.cs index 52fc7a4..e93cf47 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ClearDrawHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ClearDrawHandler.cs @@ -9,7 +9,7 @@ public bool Execute(VisionFunction func, VisionFlow item) { - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(() => { func.DrawCommands.Clear(); func.EditImageEdit?.Remove("DrawOverlay"); diff --git a/PLCSharpWpf/VVMs/Vision/VisionFunction.cs b/PLCSharpWpf/VVMs/Vision/VisionFunction.cs index b283406..039a127 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFunction.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFunction.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Http; + using Newtonsoft.Json; using OpenCvSharp; using PLCSharp.Core.Common; @@ -17,7 +17,7 @@ using System.Windows.Media; namespace PLCSharp.VVMs.Vision { - + /// /// 视觉功能的执行单元,包含流程步骤列表(VisionFlows)、参数集合(Params)和运行时图像缓存 /// @@ -350,7 +350,7 @@ namespace PLCSharp.VVMs.Vision public List DrawCommands { get; set; } = []; public async Task RenderDrawAsync() { - await System.Windows.Application.Current.Dispatcher.BeginInvoke(() => + await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => { if (DrawCommands == null || DrawCommands.Count <= 0) return; var commands = DrawCommands.ToList(); diff --git a/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs index c342d19..7c00de3 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs @@ -43,7 +43,7 @@ namespace PLCSharp.VVMs.Vision { SetProperty(ref _ShowMat, value); if (value == null) return; - _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() => { if (_ImgSrc != null && _ShowMat.Width == _ImgSrc.PixelWidth diff --git a/PLCSharpWpf/VVMs/Workflows/Script/CustomTextEditor.cs b/PLCSharpWpf/VVMs/Workflows/Script/CustomTextEditor.cs index d18cd9e..5e9348c 100644 --- a/PLCSharpWpf/VVMs/Workflows/Script/CustomTextEditor.cs +++ b/PLCSharpWpf/VVMs/Workflows/Script/CustomTextEditor.cs @@ -1,4 +1,4 @@ - + using ICSharpCode.AvalonEdit.Editing; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; @@ -85,38 +85,51 @@ namespace PLCSharp.VVMs.Workflows.Script Focus(); } #nullable enable +#pragma warning disable IDE0079 // 请删除不必要的忽略 +#pragma warning disable VSTHRD100 // Avoid async void methods private async void OnDataContextChanged(object? sender, DependencyPropertyChangedEventArgs args) +#pragma warning restore VSTHRD100 // Avoid async void methods +#pragma warning restore IDE0079 // 请删除不必要的忽略 { - var workingDirectory = Directory.GetCurrentDirectory(); + try + { - _host = new CustomRoslynHost(additionalAssemblies: - [ - Assembly.Load("RoslynPad.Roslyn.Windows"), + var workingDirectory = Directory.GetCurrentDirectory(); + + _host = new CustomRoslynHost(additionalAssemblies: + [ + Assembly.Load("RoslynPad.Roslyn.Windows"), Assembly.Load("RoslynPad.Editor.Windows"), ], - RoslynHostReferences.NamespaceDefault.With(assemblyReferences: - [ - typeof(object).Assembly, + RoslynHostReferences.NamespaceDefault.With(assemblyReferences: + [ + typeof(object).Assembly, typeof(Mat).Assembly, typeof(App).Assembly, - ] - )); + ] + )); - var documentId = await InitializeAsync(_host, new ClassificationHighlightColors(), - workingDirectory, string.Empty, SourceCodeKind.Script); + var documentId = await InitializeAsync(_host, new ClassificationHighlightColors(), + workingDirectory, string.Empty, SourceCodeKind.Script); - _document = _host.GetDocument(documentId); + _document = _host.GetDocument(documentId); - // 如果有初始化前暂存的文本,现在应用 - if (_pendingText != null && Document != null) + // 如果有初始化前暂存的文本,现在应用 + if (_pendingText != null && Document != null) + { + Document.Text = _pendingText; + Text = _pendingText; + _pendingText = null; + } + + } // 原有的代码 + + catch (Exception) { - Document.Text = _pendingText; - Text = _pendingText; - _pendingText = null; - } + } } #nullable disable @@ -210,7 +223,7 @@ namespace PLCSharp.VVMs.Workflows.Script set => SetValue(FontSizeProperty, value); } - public static readonly DependencyProperty FontSizeProperty = + public new static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register( nameof(FontSize), typeof(double),