Reputation: 457
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.
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
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
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.
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