WPF窗口关闭

时间:2010-03-09 19:13:46

标签: wpf

我希望在窗口关闭之前在服务器上发送一些数据。我使用事件结束,但它并没有结束。哪里有问题?

    private void Window_Closing(object sender, RoutedEventArgs e)
    {
            _obj.CloseConnection();

    }

2 个答案:

答案 0 :(得分:6)

尝试在后面的窗口代码中覆盖OnClosing。如果您通过设置e.Cancel = true还有其他事可做,那么您有机会停止关闭窗口。

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
       bool isClosed = _obj.CloseConnection();

       if(!isClosed)
          e.Cancel = true;

    }

答案 1 :(得分:0)

您是否检查过_obj.CloseConnection()是否存在问题?尝试调试代码并检查是否调用了事件处理程序。