This commit is contained in:
GukSang.Jin
2026-03-14 17:12:25 +08:00
parent eebf94c3a8
commit 3336dd08ab
2 changed files with 67 additions and 46 deletions

View File

@@ -1480,7 +1480,7 @@ namespace 全自动水压检测仪
// windowInstance.Show();
//}
private void SwitchWindow<T>(T windowInstance, Func<T> createFunc) where T : UIForm
private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
{
_isSwitchingWindow = true;
_readTimer?.Stop();
@@ -1497,44 +1497,55 @@ namespace 全自动水压检测仪
{
MessageBox.Show("TCP连接已断开请重新连接", "提示");
});
ResumeAfterChildWindow();
return;
}
}
// 需要将 windowInstance 声明为局部变量
T localInstance = windowInstance;
// 复用窗口实例
if (localInstance == null || localInstance.IsDisposed)
if (windowInstance == null || windowInstance.IsDisposed)
{
localInstance = createFunc();
localInstance.FormClosed += (s, e) =>
windowInstance = createFunc();
windowInstance.FormClosed += (s, e) =>
{
SafeInvoke(() =>
{
_isSwitchingWindow = false;
_readTimer?.Start();
_readTimerTwo?.Start();
_alarmMonitorTimer?.Start();
this.Show();
this.Activate();
});
ResumeAfterChildWindow();
};
}
else
{
SafeInvoke(() => localInstance.Activate());
return;
}
T targetWindow = windowInstance;
SafeInvoke(() =>
{
this.Hide();
localInstance.Show();
if (!targetWindow.Visible)
{
targetWindow.Show();
}
targetWindow.Activate();
});
}
// 如果需要更新外部引用,可以返回实例
// return localInstance;
public void ResumeAfterChildWindow()
{
SafeInvoke(() =>
{
_isSwitchingWindow = false;
if (_modbusMaster != null)
{
_readTimer?.Start();
_readTimerTwo?.Start();
_alarmMonitorTimer?.Start();
}
if (!this.Visible)
{
this.Show();
}
this.Activate();
UpdateControlsVisibilityByMode();
});
}
private void NormalTemperatureMode_FormClosing(object sender, FormClosingEventArgs e)
@@ -1683,13 +1694,13 @@ namespace 全自动水压检测仪
private void EnterFunction()
{
// 长按后进入的功能
SwitchWindow(_coeffiicientsetting, () => new Coeffiicientsetting());
SwitchWindow(ref _coeffiicientsetting, () => new Coeffiicientsetting());
}
//切换报告界面
private void uiButton1_Click(object sender, EventArgs e)
{
SwitchWindow(_report, () => new Report(CurrentReport));
SwitchWindow(ref _report, () => new Report(CurrentReport));
}
//箱体温度设置
@@ -1841,7 +1852,7 @@ namespace 全自动水压检测仪
//返回录入系统
private void uiButton7_Click(object sender, EventArgs e)
{
SwitchWindow(_scanImport, () => new ScanImport());
SwitchWindow(ref _scanImport, () => new ScanImport(this));
}
@@ -2203,4 +2214,4 @@ namespace 全自动水压检测仪
});
}
}
}
}

View File

@@ -18,8 +18,14 @@ namespace 全自动水压检测仪
private ConductivityRepository _repository;
public ScanImport()
: this(null)
{
}
public ScanImport(NormalTemperatureMode normalTemperatureMode)
{
InitializeComponent();
_normalTemperatureMode = normalTemperatureMode;
_repository = new ConductivityRepository();
uiDataGridView1.AutoGenerateColumns = false;
}
@@ -419,17 +425,13 @@ namespace 全自动水压检测仪
}
}
}
private void SwitchWindow<T>(T windowInstance, Func<T> createFunc) where T : UIForm
private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
{
// 需要将 windowInstance 声明为局部变量
T localInstance = windowInstance;
// 复用窗口实例
if (localInstance == null || localInstance.IsDisposed)
if (windowInstance == null || windowInstance.IsDisposed)
{
localInstance = createFunc();
localInstance.FormClosed += (s, e) =>
windowInstance = createFunc();
windowInstance.FormClosed += (s, e) =>
{
SafeInvoke(() =>
{
@@ -438,24 +440,32 @@ namespace 全自动水压检测仪
});
};
}
else
{
SafeInvoke(() => localInstance.Activate());
return;
}
T targetWindow = windowInstance;
SafeInvoke(() =>
{
this.Hide();
localInstance.Show();
if (!targetWindow.Visible)
{
targetWindow.Show();
}
targetWindow.Activate();
});
// 如果需要更新外部引用,可以返回实例
// return localInstance;
}
private void uiButton1_Click(object sender, EventArgs e)
{
SwitchWindow(_normalTemperatureMode, () => new NormalTemperatureMode());
if (_normalTemperatureMode != null && !_normalTemperatureMode.IsDisposed)
{
SafeInvoke(() =>
{
this.Hide();
_normalTemperatureMode.ResumeAfterChildWindow();
});
return;
}
SwitchWindow(ref _normalTemperatureMode, () => new NormalTemperatureMode());
}