我有一个 WPF 窗口,其中包含一些创建 TCP 连接的资源:
<HMIDesign:HMIBaseWindow.Resources>
<DataSource:MyDataSource x:Key="DataCtx1" />
</HMIDesign:HMIBaseWindow.Resources>
但是当我关闭承载表单的设计器窗口时,连接仍然打开,因此,如果我再次打开表单,一个新的 TCP 连接将启动。
在这种情况下如何处理断开连接?
最佳答案
使用窗口上的关闭事件应该可以做到这一点。
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Resources.Contains["DataCtx1"])
{
MyDataSource dataSource = Resources["DataCtx1"] as MyDataSource;
if (dataSource != null)
{
dataSource.Close();
}
}
}
关于c# - WPF Xaml Designer - 在设计时处理资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23609262/