microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
Question: I'm not seeing the Closing event in the Window component.
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">
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
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?
@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?
@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.
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.
Definitally would like to see this implemented!
There is AppWindow.Closing Event
But not Window.Closing... Window Events
But not Window.Closing... Window Events
It is the same thing : you can intercept it to prevent closing
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.
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.
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!