[英]WPF Xaml Designer - Dispose resource at Design Time
I have a WPF window with some resources that create a TCP connection: 我有一个WPF窗口,其中包含创建TCP连接的一些资源:
<HMIDesign:HMIBaseWindow.Resources>
<DataSource:MyDataSource x:Key="DataCtx1" />
</HMIDesign:HMIBaseWindow.Resources>
But when I close the designer window that host the Form, the connection remains open and so, if I open the form again a new TCP connection will start. 但是,当我关闭承载该窗体的设计器窗口时,该连接仍保持打开状态,因此,如果再次打开该窗体,则将启动新的TCP连接。
How can I handle the Disconnection in this scenario? 在这种情况下如何处理断开连接?
It should be possible to do this using the Closing Event on the Window. 应该可以使用窗口上的关闭事件来执行此操作。
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.