Felix
Felix

Reputation: 4081

How to close/hide app main window?

I have an app that has a main window and secondary windows.

In certain situations, I would like to only display a secondary window when the user launches the app.

The problem is that the app's main window is automatically launched, so I after programamtically opening the secondary window I am left with both the main and secondary windows.

Is there any way I can hide the main window? (Ie, emulate the user pressing the "close button" in the top right corner of the main window?)

Upvotes: 3

Views: 1301

Answers (3)

Soumya Mahunt
Soumya Mahunt

Reputation: 2762

Execute ApplicationView.GetForCurrentView().TryConsolidateAsync() in main window to close it properly as if you have closed it by clicking close button in title bar. This is preferred over Application.Exit() as this method suspends the app while Application.Exit() closes app abruptly. Also closing app by this method your app's previous position and size is remembered unlike for Application.Exit().

Upvotes: 0

Marian Dolinský
Marian Dolinský

Reputation: 3492

Why closing/hiding the main window? You could show the content of the secondary window in the main window (that is created when app's launched) and if user wants to show the main window you could show its content in secondary window.

Upvotes: 1

Felix
Felix

Reputation: 4081

After creating the secondary view I switch to this new view specifying the fromViewId as the main view id, and using the ApplicationViewSwitchingOptions.ConsolidateViews option, which hides it:

// Create seprate secondary window
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(secondaryWindowViewId);

// Switch to secondary window just created, which hides the main view
await ApplicationViewSwitcher.SwitchAsync(
    secondaryWindowViewId, 
    mainWindowViewId, 
    ApplicationViewSwitchingOptions.ConsolidateViews);

Not the most elegant solution, but it works :) More elegant solutions are welcome!

Upvotes: 2

Related Questions