This commit is contained in:
xyy
2026-03-16 15:36:54 +08:00
parent 533ca5a0fd
commit e941113da9
9 changed files with 681 additions and 273 deletions

View File

@@ -386,44 +386,106 @@ namespace 自救器呼吸器综合检验仪
private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : Window, new()
{
// 1. 停止当前窗口的定时器(不释放资源)
// 停止当前窗口的定时器(不释放资源)
_readTimer?.Stop();
// 2. 检查资源是否可用(添加重连机制)
// 检查资源是否可用(添加重连机制)
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
{
// 尝试重新连接
bool reconnectSuccess = TryReconnect();
if (!reconnectSuccess)
if (!TryReconnect())
{
MessageBox.Show("TCP连接已断开请重新连接", "提示");
return;
}
}
// 3. 复用窗口实例:不存在则创建,存在则激活
// 创建一个简易的加载覆盖窗口,作为状态栏/遮罩,避免白屏
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
// 取消遮罩背景色,使用完全透明背景以避免暗色遮罩
Background = Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
// 内容:底部居中的状态文本(不遮挡背景)
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = Brushes.Black,
Background = Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
// 显示遮罩
overlay.Show();
void ShowTargetWindow(T target)
{
// 如果目标已可见,直接激活并隐藏当前
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
target.Opacity = 0;
void OnContentRendered(object s, EventArgs e)
{
target.ContentRendered -= OnContentRendered;
var anim = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(200));
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
target.Show();
}
// 创建或复用窗口实例
if (windowInstance == null)
{
windowInstance = createFunc();
// 添加窗口关闭事件处理
windowInstance.Closed += (s, args) =>
{
// 窗口关闭时重新启动定时器并显示当前窗口
_readTimer?.Start();
//this.Show();
this.Activate();
};
ShowTargetWindow(windowInstance);
}
else
{
// 激活已存在的窗口(前置显示)
windowInstance.Activate();
//return;
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
// 4. 切换窗口:隐藏当前窗口,显示目标窗口(非模态)
this.Hide();
windowInstance.Show(); // 使用 Show() 而不是 ShowDialog()
}
// 添加重连方法

View File

@@ -490,12 +490,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = Brushes.Black,
Background = Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -507,6 +543,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -529,6 +566,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
// 添加重连方法
private bool TryReconnect()
{

View File

@@ -463,12 +463,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = Brushes.Black,
Background = Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -480,6 +516,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -502,6 +539,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
// 添加重连方法
private bool TryReconnect()
{

View File

@@ -308,12 +308,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = Brushes.Black,
Background = Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -325,6 +361,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -347,6 +384,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
private bool TryReconnect()
{
try

View File

@@ -131,12 +131,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = System.Windows.Media.Brushes.Black,
Background = System.Windows.Media.Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -148,6 +184,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -171,6 +208,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
// 添加重连方法

View File

@@ -272,12 +272,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = System.Windows.Media.Brushes.Black,
Background = System.Windows.Media.Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -289,6 +325,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -311,6 +348,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
//ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{

View File

@@ -290,12 +290,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = System.Windows.Media.Brushes.Black,
Background = System.Windows.Media.Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -307,6 +343,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -329,6 +366,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
//ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{

View File

@@ -10,6 +10,7 @@ using System.Text;
using System.Windows;
using System.Windows.Controls;
using .Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace
@@ -288,12 +289,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = System.Windows.Media.Brushes.Black,
Background = System.Windows.Media.Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -305,6 +342,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -327,6 +365,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
//ShowErrorMsg($"切换窗口失败:{ex.Message}");
}
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{

View File

@@ -10,6 +10,7 @@ using System.Text;
using System.Windows;
using System.Windows.Controls;
using .Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace
@@ -329,12 +330,48 @@ namespace 自救器呼吸器综合检验仪
}
}
Window overlay = null;
try
{
overlay = new Window
{
Owner = this,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
ShowInTaskbar = false,
ResizeMode = ResizeMode.NoResize,
Width = this.ActualWidth,
Height = this.ActualHeight,
Left = this.Left,
Top = this.Top,
ShowActivated = false,
Topmost = true
};
var grid = new Grid();
var tb = new TextBlock
{
Text = "正在加载,请稍候...",
Foreground = System.Windows.Media.Brushes.Black,
Background = System.Windows.Media.Brushes.Transparent,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(0, 0, 0, 20),
FontSize = 16
};
grid.Children.Add(tb);
overlay.Content = grid;
overlay.Show();
void ShowTargetWindow(T target)
{
if (target.IsVisible)
{
target.Activate();
this.Hide();
overlay?.Close();
return;
}
@@ -346,6 +383,7 @@ namespace 自救器呼吸器综合检验仪
target.BeginAnimation(Window.OpacityProperty, anim);
this.Hide();
target.Activate();
overlay?.Close();
}
target.ContentRendered += OnContentRendered;
@@ -368,6 +406,12 @@ namespace 自救器呼吸器综合检验仪
ShowTargetWindow(windowInstance);
}
}
catch (Exception ex)
{
overlay?.Close();
MessageBox.Show($"切换窗口失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{