How do you tell if a WPF Window is closed?

前端 未结 7 2011
情深已故
情深已故 2020-12-09 07:27

I\'m working on an application that displays some child windows which can either be closed by the user or are automatically closed. While debugging some exceptions that wer

7条回答
  •  [愿得一人]
    2020-12-09 07:41

    I don't know why the IsDisposed property is internal, but if you don't fear reflection:

    var window = new Window();
    var propertyInfo = typeof(Window).GetProperty("IsDisposed", BindingFlags.NonPublic | BindingFlags.Instance);
    var isDisposed = (bool)propertyInfo.GetValue(window);
    

    That being said, reflection is not to be overused because you're no longer protected by the public API of the class. Be sure to use at least unit tests if you go that route.

提交回复
热议问题