microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Question: I'm not seeing the Closing event in the Window component.

Open germanfortiz opened this issue 3 years ago • 9 comments

Testing Project Reunion 0.5, I'm not able to stop the closing of my application because there is not a Closing event. May be i'm missing something or is just this Window event is not yet available?

<Window
    x:Class="WinUI3Desktop.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUI3Desktop"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Activated="Window_Activated"
    Closing="Window_Closing"  <--- Not available
    Closed="Window_Closed"
    VisibilityChanged="Window_VisibilityChanged"
    mc:Ignorable="d">

germanfortiz avatar May 22 '21 14:05 germanfortiz

The event is not available in WinUI 3, but you can implement it by yourself using the Win32 APIs. For example: https://github.com/marb2000/DesktopWindow

marb2000 avatar May 24 '21 21:05 marb2000

Thank you for the tip.! Just curious, in future WinUI3 Window implementations, we will have to implement this event as an extension, as you suggested?

germanfortiz avatar May 25 '21 23:05 germanfortiz

@marb2000 do you have thoughts on germanfortiz's question above. I'm moving a WPF app to WinUI3 and I'm missing this Closing event that WPF had. Can we expect one in the future?

pushkin- avatar Feb 11 '22 22:02 pushkin-

@marb2000

Thanks for writing the DesktopWindow extension code.

WinUI 3 is now 1.0.2 and there is still no Closing event.

So, referring to DesktopWindow, I tried to intercept the window message and implement the close event directly, but there is a problem in the basic structure.

(It works normally in Packaged.)

When unpackaged, it does not work with the following exception.

image

The code below is the reproduced code. https://github.com/dimohy/csharp-check/tree/main/WinUI3/WinUI3UnpackagedWinProcTest

If you are still working on this and know the cause, please help.

dimohy avatar Apr 15 '22 05:04 dimohy

Definitally would like to see this implemented!

jamers99 avatar Jul 14 '22 20:07 jamers99

There is AppWindow.Closing Event

castorix avatar Jul 15 '22 00:07 castorix

But not Window.Closing... Window Events

germanfortiz avatar Jul 15 '22 14:07 germanfortiz

But not Window.Closing... Window Events

It is the same thing : you can intercept it to prevent closing

castorix avatar Jul 15 '22 16:07 castorix

Thanks for the solution, it works fine!

In App.OnLaunched() add: m_window.Closed += Window_Closed;

in Window_Closed(object sender, WindowEventArgs args) to stop the closing of the window do:
args.Handled = true;

-Exit the application from a window or a page: Application.Current.Exit(); will execute the Window_Closed callback.

yvlawy avatar Aug 07 '22 08:08 yvlawy

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 28 '23 02:07 github-actions[bot]

With the help of @castorix and @yvlawy, I finally solved this issue:

In the App.OnLaunched event, I added this:

  protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
  {
        mainWindow = new MainWindow()
        {
            Title = this.GetVersionDescription(),
        };

        mainWindow.Activate();

        // Get the handle of the created window.
        this.windowhandle = PInvoke.User32.GetActiveWindow();

        // Retrieve the WindowId that corresponds to hWnd.
        Microsoft.UI.WindowId windowId =
            Microsoft.UI.Win32Interop.GetWindowIdFromWindow(this.windowhandle);

        // Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.
        Microsoft.UI.Windowing.AppWindow appWindow =
            Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);

        if (appWindow != null)
        {
            appWindow.Closing += this.AppWindow_Closing;
        }
  }

Finally, to stop the closing event, I added the following in the AppWindow_Closing function:

  private void AppWindow_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
  {
        if (somethingIsRunningAndTheUserCanNotCloseTheApp())
        {
              args.Cancel = true; // This prevents the application to close.
        }
  }

Thanks!

germanfortiz avatar Jul 28 '23 11:07 germanfortiz