Sethu Bala
Sethu Bala

Reputation: 457

How to recover main view of UWP app when main view of the app is closed but secondary view is still open and opening the app from start menu?

I am developing a UWP app with multiple views. (Say I open my app's settings in a separate window.) I need to recover my app's main view in the following scenario.

  1. Open the app.
  2. Open app settings in a secondary view.
  3. Close the main view of the app and leave the settings window open.
  4. Open the app from start menu or by clicking on the app's shortcut icon.

The above action takes me to the already opened secondary view(settings window). But I need to bring up the main view of the app.

In my App.xaml.cs file, OnLaunched method, CoreApplication.GetCurrentView().IsMain gives me true but CoreApplication.GetCurrentView().IsHosted gives me false. I tried with CoreApplication.GetCurrentView().CoreWindow.Activate() & Window.Current.Activate(); but both didn't help, it just takes me to the secondary view(settings window) which is left open.

How to bring up the main view of my UWP app?

Upvotes: 2

Views: 621

Answers (2)

Sethu Bala
Sethu Bala

Reputation: 457

I found the solution to my problem. My requirement was, when the app's main window has been closed, but a secondary window is still open, and the user tries to open the app from the start menu or by clicking the app's icon, I should bring up the main view of the app in a separate window, without closing the secondary view/window where the user currently is. In other words, just bring the main window of the app in front and let the secondary window be there as it is behind the main window. So now we will have 2 windows on screen:

1.Main window

2.Already opened secondary window.

I achieved this by the below code.

In the App.xaml.cs file, OnLaunched(LaunchActivatedEventArgs e) method, the argument LaunchActivatedEventArgs e has a property named CurrentlyShownApplicationViewId, which gives the id of the view, where the user currently is (if any of the app view is open).

ApplicationView.GetApplicationViewIdForWindow(CoreApplication.GetCurrentView().CoreWindow) gives the id of the main view of the app.

With these 2 id's in hand, I am able to bring up my app's main window, with the help of the below code.

Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
 await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
       ApplicationView.GetApplicationViewIdForWindow(
       CoreApplication.GetCurrentView().CoreWindow), 
       ViewSizePreference.Default, e.CurrentlyShownApplicationViewId, 
       ViewSizePreference.Default);
});

ApplicationViewSwitcher.TryShowAsStandaloneAsync brings back my app's main window in front, in a separate window, leaving behind the secondary window undisturbed. So now the user has 2 windows - the app's main window as well as the existing secondary window.

Upvotes: 2

Nico Zhu
Nico Zhu

Reputation: 32775

For your requirement, you could refer MultipleViews official code sample. And it has switch to main view action. you could use it to switch to main view event if the main view closed.

enter image description here

private async void GoToMain_Click(object sender, RoutedEventArgs e)
{
    // Switch to the main view without explicitly requesting
    // that this view be hidden
    thisViewControl.StartViewInUse();
    await ApplicationViewSwitcher.SwitchAsync(mainViewId);
    thisViewControl.StopViewInUse();
}

Upvotes: 2

Related Questions