修复设计模式下控件连接设备的问题:添加DesignMode条件检查,仅在非设计模式下初始化设备管理器并连接设备

This commit is contained in:
zqm
2025-10-29 15:12:44 +08:00
parent 13da804dd2
commit 5517ef2814
2 changed files with 64 additions and 18 deletions

View File

@@ -19,6 +19,33 @@ namespace JoyD.Windows.CS.Toprie
// 是否正在接收图像
private bool _isReceivingImage = false;
// 项目路径,用于数据文件的存取
private string _projectPath = "";
/// <summary>
/// 获取或设置项目路径,控件所需的数据文件将在此目录中进行存取
/// </summary>
[Category("配置")]
[Description("设置项目路径,控件所需的数据文件将在此目录中进行存取")]
[DefaultValue("")]
public string ProjectPath
{
get { return _projectPath; }
set
{
// 只有当值发生变化时才进行同步
if (_projectPath != value)
{
_projectPath = value;
// 如果DeviceManager已经初始化则同步更新其ProjectPath属性
if (_deviceManager != null)
{
_deviceManager.ProjectPath = _projectPath;
}
}
}
}
// 显示错误的定时器
private System.Windows.Forms.Timer _errorDisplayTimer;
@@ -39,6 +66,12 @@ namespace JoyD.Windows.CS.Toprie
try
{
string logFile = Path.Combine(Application.StartupPath, "log.txt");
// 确保日志文件目录存在
string logDir = Path.GetDirectoryName(logFile);
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
if (File.Exists(logFile))
{
File.WriteAllText(logFile, string.Empty);
@@ -77,16 +110,17 @@ namespace JoyD.Windows.CS.Toprie
/// 初始化设备管理器
/// </summary>
private void InitializeDeviceManager()
{
// 只有在非设计模式下才初始化设备管理器
if (!DesignMode)
{
_deviceManager = new DeviceManager
{
AutoReconnectEnabled = true,
ReconnectInterval = 2000 // 2秒
ReconnectInterval = 2000, // 2秒
ProjectPath = !string.IsNullOrEmpty(ProjectPath) ? ProjectPath : Application.StartupPath
};
// 确保DeviceManager的设计模式状态与控件一致
DeviceManager.IsDesignMode = DesignMode;
// 设置静态属性
DeviceManager.MaxReconnectAttempts = 5;
@@ -98,8 +132,7 @@ namespace JoyD.Windows.CS.Toprie
// 注册连接异常事件
_deviceManager.ConnectionException += DeviceManager_ConnectionException;
}
}
/// <summary>

View File

@@ -153,6 +153,9 @@ namespace JoyD.Windows.CS.Toprie
// 设计模式标志,用于在设计模式下跳过实际的设备连接和初始化
public static bool IsDesignMode { get; set; } = false;
// 项目路径,用于数据文件的存取
private string _projectPath = "";
// A8SDK实例
private A8SDK _a8Sdk;
// 设备ID列表
@@ -243,6 +246,16 @@ namespace JoyD.Windows.CS.Toprie
private Thread _imageReconnectThread;
private Stream _imageStream;
/// <summary>
/// 项目路径属性
/// 用于设置控件存取数据文件的目录
/// </summary>
public string ProjectPath
{
get { return _projectPath; }
set { _projectPath = value; }
}
/// <summary>
/// 检查网络是否可用
/// </summary>