I found this SO question, and also a relevant answer here, but I am still getting exceptions when I try to implement multiple windows using Template 10.
(I also found this SO question, which appears to recommend jumping out of Template 10 altogether, and using a Frame directly. When I tried the code in the answer, I got a CLR exception. So I abandoned that approach and went back to the other questions).
As far as I understand from the descriptions here and here, you need to create a new frame and navigation service, assign the navigation service to your frame, and then use the navigation service to navigate to the new page.
I tried this code in the ViewModel of the page navigated from, but it gets exception 0xE0434352 at the first line when creating the frame.
Frame secondaryFrame = new Frame(); //--->Exception 0xE0434352
var secondaryNav = BootStrapper.Current.NavigationServiceFactory(BootStrapper.BackButton.Attach, BootStrapper.ExistingContent.Exclude, secondaryFrame);
secondaryNav.Navigate(typeof(MySecondaryPage));
Window.Current.Content = secondaryFrame; //activation
Why does creating a Frame get an exception?
And is the above code correct for opening a secondary window?
EDIT: Thanks to mvermef's answer on this question, I have now found the UWP sample for multiple windows on GitHub. It is available in version 1.1.13p of the UWP samples, in /Samples/MultipleViews/ViewModels/, here.
My original attempt at opening a secondary window using NavigationService.OpenAsync() was the same as the corresponding line of code in the sample. This is the function:
private async void MyEventHandler(bool openSecondaryWindow)
{
await DispatcherWrapper.Current().DispatchAsync(async () =>
{
if (openSecondaryWindow)
{
try
{
//the next line gets exception 0xE0434352
var control = await NavigationService.OpenAsync(typeof(MySecondaryPage), null, Guid.NewGuid().ToString());
control.Released += Control_Released;
}
catch (Exception ex)
{
}
}
});
}
It still gets exception 0xE0434352. I have tried it with my secondary page, with another page that usually opens without a problem, and with a blank page that I created. All attempts get the same exception.
I discovered what seems to have caused the exception.
The NavigationService was null when referenced simply as "NavigationService
" as in the sample.
I had to reference it as BootStrapper.Current.NavigationService
.
Here is the code that works:
private async void MyEventHandler(bool openSecondaryWindow)
{
await DispatcherWrapper.Current().DispatchAsync(async () =>
{
if (openSecondaryWindow)
{
try
{
var control = await BootStrapper.Current.NavigationService.OpenAsync(typeof(MySecondaryPage), null, Guid.NewGuid().ToString());
control.Released += Control_Released;
}
catch (Exception ex)
{
}
}
});
}
(The module was included with "using" and as a reference, and obviously I had clean/built several times)