Can we know if a window has been closed by the user or code on WPF?

后端 未结 2 1253
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 11:53

I have a window control that I show on my app. Sometimes, the window is closed by the user (clicking on the \'X\' button) and sometimes automatically by code.

I\'m list

相关标签:
2条回答
  • 2021-01-25 12:37

    Option 1: Define a new type

     public class CodeClosingEventArgs : EventArgs
     { 
      // Fill any custom data you want
     }
    

    And then call OnClosed(new CodeClosingEventArgs()); explicit in your code and check for the Event Type, if its a "normal" event arg, its the user by clicking the 'X'.

    Option 2:

    use a custom method which closes your form.

     public void MyClosingFormMethod()
     {
        base.OnClosed(null); 
     }
    
    0 讨论(0)
  • 2021-01-25 12:51

    Why not create a boolean value and then set it if the certain activity occurs?

    bool closedByUser = false;
    

    and then where the code is meant to close the form just add closedByUser = false; and closedByUser = true; if the action is user derived.

    0 讨论(0)
提交回复
热议问题