1

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.

user1725145
  • 3,993
  • 2
  • 37
  • 58

2 Answers2

0

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.

You have different ways to implement this behavior with NavigationService OpenAsync method.

await NavigationService.OpenAsync(typeof(Views.TestPage));

You could also use ViewService OpenAsync method.

var viewservice = new ViewService();
await viewservice.OpenAsync(typeof(Views.TestPage));

Please make sure that it was invoked in the UI thread.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Thank you for the reply. Should I create a second NavigationService? – user1725145 Oct 30 '17 at 11:30
  • Yep, If you set window current content as `secondaryFrame`, Although you could create multiple windows, the original window content will become `secondaryFrame`. It will be difficult to manage. – Nico Zhu Oct 31 '17 at 01:54
  • Sorry, I find this answer and comment very difficult to understand. I want to open a second window with different content, while leaving the main window unchanged. I have already tried using the OpenAsync function of the first NavigationService to open a second window, and it didn't work. Are you saying that this was correct? My code was in a ViewModel, so maybe threads were the issue. – user1725145 Oct 31 '17 at 07:40
  • Could you share a sample to reproduce this issue? – Nico Zhu Oct 31 '17 at 07:42
  • I have tested your code in the ViewModel. It works in my side. – Nico Zhu Oct 31 '17 at 09:38
  • Please try to navigate to a blank page. – Nico Zhu Oct 31 '17 at 09:39
  • As I posted above, I have already tried it with a blank page and it gets the same exception. I'll try a few more variations, like moving the code to another view model, and trying to get more information about the exception. – user1725145 Oct 31 '17 at 09:55
  • Our normal pages are displayed inside a Shell, could that make a difference? – user1725145 Oct 31 '17 at 10:19
  • I moved the code from the ShellViewModel to the constructor of the AboutPageViewModel, it still gives an exception when trying to open a blank page. – user1725145 Oct 31 '17 at 10:49
  • I discovered that this exception is just a fancy null reference exception for the NavigationService - please see my answer under. Not sure why this happened. Thank you for your time anyway! – user1725145 Oct 31 '17 at 14:12
0

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)

user1725145
  • 3,993
  • 2
  • 37
  • 58