1

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.

Frank
  • 136
  • 1
  • 10

2 Answers2

1

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();                 
                
        };
    }
Junjie Zhu - MSFT
  • 2,086
  • 1
  • 2
  • 6
0

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.

Frank
  • 136
  • 1
  • 10