Search code examples
c#windows-runtimewindows-phonewindows-phone-8.1

App can't Navigate to MainPage


I'm facing the strange issue on my WP8.1 RT app - one of my beta testers reported that he sees black screen just after the splash screen. The app doesn't crash, hang the phone or other - just black screen instead of MainPage. I've implemented some Trace methods inside the code to track the issue. The code looks like this:

// OnLaunched method that gets called when the App starts
protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    await Trace.WriteLineAsync(true, "Launched");
    Frame rootFrame = CreateRootFrame();
    await Trace.WriteLineAsync(true, "Before - better checkup");
    if (rootFrame.Content == null)
    {
        await Trace.WriteLineAsync(false, "Rootframe content was null, navigating");
        if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
            await Trace.WriteLineAsync(false, "Navigation false");
        else await Trace.WriteLineAsync(false, "Navigation was ok");
    }
    await Trace.WriteLineAsync(true, "After - better checkup");
    Window.Current.Activate();
} 

// Method creating the rootFrame:
private Frame CreateRootFrame()
{
    Trace.WriteLineAsync(true, "Create Root frame");
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        Trace.WriteLineAsync(true, "Root frame was null");
        rootFrame = new Frame();
        SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
        rootFrame.NavigationFailed += OnNavigationFailed;
        Window.Current.Content = rootFrame;
        Trace.WriteLineAsync(false, "Window content {0}", Window.Current.Content.ToString());
    }
    return rootFrame;
}

The log the beta tester gets looks like this:

2014-10-06 13:02:56: Launched
2014-10-06 13:02:56: Create Root frame
2014-10-06 13:02:56: Root frame was null
Window content Windows.UI.Xaml.Controls.Frame
2014-10-06 13:02:57: Before - better checkup
Rootframe content was null, navigating
Navigation false
2014-10-06 13:02:57: After - better checkup
2014-10-06 13:03:01: App.cs suspending event

As you can see the most important line is Navigation false which means that

if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
     await Trace.WriteLineAsync(false, "Navigation false");

Navigation returns false - I cannot navigate within my Frame, no crash, no hang - (Suspending event works), just can't get to MainPage. I have complately no idea what can be the source of the problem. Moreover - on my phone everything works, other devices also - both debug & release.

Does anybody have an idea why I cannot navigate to my MainPage?

Edit - navogation failed event added:

void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
    Trace.WriteLineAsync(true, "Failed to load page");
    throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}

Solution

  • Thanks to Nate Diamond comment I started searching for the cause of the problem little different way. I add this answer as it may help someone one day.

    Finally after many attempts and help from the beta tester it turned out that the problem was caused by one of the variables that was initialized before the constructor of the Page. The initialization resulted in exception which was swallowed by Navigate method and thus it returned false.

    The other thing is why the initialization resulted in exception, but that's the other story.