增强窗口关闭处理和资源清理

在 `LoginWindow.xaml` 中添加了 `Closing` 事件处理程序,以便在窗口关闭时执行特定逻辑。新增的 `Window_Closing` 方法会检查 `MainWindow.token` 是否为 `null`,并在必要时关闭应用程序。此外,在 `MainWindow.xaml.cs` 中确保在清理资源时将 `token` 设置为 `null`,以避免潜在的资源泄漏或错误。
This commit is contained in:
绪山七寻 2025-06-07 16:17:28 +08:00
parent 424311f088
commit 15147b88a6
3 changed files with 6 additions and 1 deletions

View File

@ -8,7 +8,7 @@
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="chatclient.LoginWindow" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="chatclient.LoginWindow"
mc:Ignorable="d" mc:Ignorable="d"
Title="LoginWindow" Height="590" Width="360" MinHeight="590" MinWidth="360" MaxHeight="590" MaxWidth="360" Title="LoginWindow" Height="590" Width="360" MinHeight="590" MinWidth="360" MaxHeight="590" MaxWidth="360"
ResizeMode="NoResize"> ResizeMode="NoResize" Closing="Window_Closing">
<TabControl> <TabControl>
<TabItem Header="登录账号" Cursor="Hand" Height="40"> <TabItem Header="登录账号" Cursor="Hand" Height="40">

View File

@ -213,6 +213,10 @@ namespace chatclient
// 例如UserName = LoadSavedUsername(); // 例如UserName = LoadSavedUsername();
// Update("UserName"); // Update("UserName");
} }
public void Window_Closing(object sender, CancelEventArgs e)
{
if(MainWindow.token == null) Application.Current.Shutdown();
}
} }
} }

View File

@ -339,6 +339,7 @@ namespace chatclient
Client?.Shutdown(SocketShutdown.Both); Client?.Shutdown(SocketShutdown.Both);
Client?.Close(); Client?.Close();
Client?.Dispose(); Client?.Dispose();
token = null;
_trayManager?.Dispose(); _trayManager?.Dispose();
} }
} }