Why does unloaded event of window do not fire in WPF?

后端 未结 3 497
滥情空心
滥情空心 2021-01-17 18:26

In my WPF application I have created a window and show it as a dialog by calling it by the method ShowDialog(). But when I close the window by Close() method the Unloaded ev

3条回答
  •  逝去的感伤
    2021-01-17 18:36

    if you really want to get confirmation of closing i think its best to understand the life-cycle of a window and the relevant events it raises.

    However the in my opinion the best source of confirmation is the Closed Event. Other framework ways maybe unreliable

    Closing Events

    When a window closes, it raises two events: Closing and Closed.

    Closing is raised before the window closes, and it provides a mechanism by which window closure can be prevented. One common reason to prevent window closure is if window content contains modified data. In this situation, the Closing event can be handled to determine whether data is dirty and, if so, to ask the user whether to either continue closing the window without saving the data or to cancel window closure. The following example shows the key aspects of handling Closing.

    Further more

    The Closing event handler is passed a CancelEventArgs, which implements the BooleanCancel property that you set to true to prevent a window from closing. +

    If Closing is not handled, or it is handled but not canceled, the window will close. Just before a window actually closes, Closed is raised. At this point, a window cannot be prevented from closing.

    Note

    While a window can be explicitly closed through mechanisms provided in the non-client and client areas, a window can also be implicitly closed as a result of behavior in other parts of the application or Windows, including the following:

    • A user logs off or shuts down Windows.

    • A window's owner closes.

    • The main application window is closed and ShutdownMode is OnMainWindowClose.

    • Shutdown is called.

    All Window Lifetime Events

    The following illustration shows the sequence of the principal events in the lifetime of a window.

    The following illustration shows the sequence of the principal events in the lifetime of a window that is shown without activation (ShowActivated is set to false before the window is shown).

提交回复
热议问题