Home > Software engineering >  Windows App SDK 1.2: How to prevent the application from shutdown when the main window is closed?
Windows App SDK 1.2: How to prevent the application from shutdown when the main window is closed?

Time:01-11

I am building a Windows App SDK app that needs to stay alive and display a tray icon when the main window is closed.

For WPF, I know this can be achieved by setting ShutdownMode="OnExplicitShutdown". And I wonder how can I do this with Windows App SDK 1.2.

Appreciate any useful information.

CodePudding user response:

I found the solution:

Hook class AppWindow's Closing event and set AppWindowClosingEventArgs.Cancel = true to prevent the window from closing. Then, call window.Hide() to hide the window.

CodePudding user response:

You can try this. AppWindow.Hide

  public MainWindow()
    {
        this.InitializeComponent();


        IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this);
        WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
        AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);

            
        Closed  =  (s, e) =>
        {                      
            e.Handled = true;
            appWindow.Hide();                 
                
        };
    }
  • Related