c# - WPF Xaml Designer - 在设计时处理资源

标签 c# wpf visual-studio tcp

我有一个 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/

相关文章:

windows - VS2005中Winnt.h文件在哪里

c# - 将 SecureString 与 LogonUser 一起使用

c# - Validator.TryValidateObject 不验证属性

c# - GetWindowLong/SetWindowLong 与重写 CreateParams?

c# - Mvvm wpf : update value binding before leaving focus of input element

.net - 按用户设置输出路径

c++ - VS2010 : Compile a C++ Project in 64 bit

c# - 通过反射加载程序集的风险

c# - 在 Entity Framework 和 WPF UI 中显示计算属性

c++ - 在 Emacs 中使用 Visual Studio 6 C++ 编译器