dumbledad
dumbledad

Reputation: 17527

Adding a page before the hub page in a Windows Phone 8.1 store app

I'm building a Windows Universal Store App, concentrating first on the Windows Phone 8.1 app. I'm basing my app on a hub app. I want to add a splash page to the app startup to replace the static splash screen with a XAML based animation. I am confused by navigation since it all seems to be set up and owned by the hub page.

I have looked at

In fact the last of those explicitly states that "hub pages are the user's entry point to the app".

How do I add pages to my hub app that the user will encounter before the hub, like a splash page or a logon screen?

Upvotes: 0

Views: 415

Answers (1)

dumbledad
dumbledad

Reputation: 17527

Typically no sooner had I posted the question than I saw where to find the answer. When NavigationHelper is added to the project along with the hub page it includes comments explaining how to use it:

To make use of NavigationHelper, follow these two steps or start with a BasicPage or any other Page item template other than BlankPage.

1) Create an instance of the NavigationHelper somewhere such as in the constructor for the page and register a callback for the LoadState and SaveState events.

public MyPage()
{
    this.InitializeComponent();
    var navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += navigationHelper_LoadState;
    this.navigationHelper.SaveState += navigationHelper_SaveState;
}

private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{ 
}

private async void navigationHelper_SaveState(object sender, LoadStateEventArgs e)
{ 
}

2) Register the page to call into the NavigationHelper whenever the page participates in navigation by overriding the Windows.UI.Xaml.Controls.Page.OnNavigatedTo and Windows.UI.Xaml.Controls.Page.OnNavigatedFrom events.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    navigationHelper.OnNavigatedTo(e);
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    navigationHelper.OnNavigatedFrom(e);
}

That boiler-plate code only required slight changes in my page: take out the async, and make navigationHelper an instance variable.

Then to start in another page follow the instructions in Set start page in Windows Phone 8.1 universal app to edit App.xaml.cs thus:

#if WINDOWS_PHONE_APP
    if (!rootFrame.Navigate(typeof(SplashPage), e.Arguments))
    {
        throw new Exception("Failed to create initial page");
    }
#endif
#if WINDOWS_APP

    if (!rootFrame.Navigate(typeof(HubPage), e.Arguments))
    {
        throw new Exception("Failed to create initial page");
    }
#endif

Then in SplashPage.xaml.cs when I need to navigate to the hub page I add

if (rootFrame.Navigate(typeof(HubPage)))
{
    Window.Current.Content = rootFrame;
}
else
{
    throw new Exception("Failed to create hub page");
}

Upvotes: 1

Related Questions