0

I've tried a number of approaches to this, but when trying to navigate between one page and another in Windows 10 I'm getting some strange behaviour. I display my first page in app.xaml.cs:

View.MainPageView mp = new View.MainPageView();
Window.Current.Content = mp;

This displays the main page fine; however, I have a command that does effectively the same thing for my second page:

View.SecondView dv = new View.SecondView();
Window.Current.Content = dv;

What happens is that it displays the second page, but it is blank. When I try to inspect it using XAML Spy, it shows that the page is there; however, there is no content for it at all (that is, no controls showing).

Is there something particular about Windows 10 that might explain this or, alternatively, is there something about overriding the content in this way that might explain this behaviour?

EDIT:

It appears that the issue is caused by using a bound command, rather than a handled even in order to navigate; the following code works on a click event:

Frame rootFrame = Window.Current.Content as Frame;

rootFrame.Navigated += RootFrame_Navigated;
rootFrame.Navigate(typeof(View.SecondView), null);            

But when called from a bound command, whilst it still shows the view, it is always blank. I initially thought this might be relating to the calling thread, but using the UI thread dispatcher makes no difference.

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269

1 Answers1

2

Windows 10 uses the same approach like 8.1.

If you create a new project in Visual Studio and open App.xaml.cs you can find there OnLaunched method. Inside this method you can see that instance of Frame is created and assigned to Window.Current.Content. Frame will help you to manage whole navigation. At the end of this method is navigation to the MainPage.

rootFrame.Navigate(typeof(MainPage), e.Arguments);

If you want to navigate to the second page from the main page, you have to use:

Frame.Navigate(typeof(SecondPage));

In order to go back to the main page just call:

if (Frame.CanGoBack)
{
    Frame.GoBack();
}
Jakub Krampl
  • 1,774
  • 1
  • 16
  • 24
  • Thanks - this has certainly led me to identify why this isn't working. For some reason, if I put the Navigate line in your answer in a bound command, it fails (that is, appears blank); but if I put it in the code behind on the _click event, it works. – Paul Michaels Nov 12 '15 at 18:22
  • @pm_2 Can you show me part of your code which does not work? – Jakub Krampl Nov 12 '15 at 19:38
  • I've updated my question with some additional sample code – Paul Michaels Nov 12 '15 at 19:59
  • @pm_2 It should work also from a bound command. Do you use any MVVM framework? – Jakub Krampl Nov 12 '15 at 20:33
  • Nope - just rolling my own... and I agree: it should work (hence the question) – Paul Michaels Nov 12 '15 at 21:11
  • @pm_2 if you use your own solution, I suppose that you somehow implemented `ICommand` your own way and maybe some problem could be there. You should maybe provide sample project. – Jakub Krampl Nov 12 '15 at 22:16
  • I tried using this example, but ended up with the following error: 'Object reference not set to an instance of an object.' – janlindso Sep 29 '16 at 17:52