如何预加载我要导航到的页面?

时间:2014-07-07 21:08:40

标签: c# xaml windows-phone-8

我正在为WP8构建应用程序,MainPage.xaml需要几秒钟才能加载。 我想添加一个动画的启动画面,所以这就是我所做的:

  • 我创建了一个新的,并将其设置为默认导航页面
  • 我在页面中放置了一个StoryBoard动画,周围没有任何其他内容,因此它可以快速加载

现在,如果我尝试从此页面导航到主页面,则需要几秒钟,因为它必须一如既往地加载所有内容。

stackoverflow上的一个用户编写了一个代码示例来做这样的事情,说如果你在导航到它之前创建一个新的页面实例,它将导致页面实际上预加载,使导航真的很快。 这是他的代码:

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
    var sb = new Storyboard();
    // create your animation here

    sb.Completed += (sender, args) => PreLoad();
    sb.Begin();
}

private void PreLoad()
{
    // this is the part that actually takes time and causes things to get loaded
    // you may need it in a try/catch block depending on what is in your constructor
    var page = new PageToNavigateTo();

    // now create an animation at the end of which we navigate away
    var sbOut = new Storyboard();
    // create your animation here

    sbOut.Completed += (sender, args) => NavigateToNextScreen();
    sbOut.Begin();
}

private void NavigateToNextScreen()
{
    // navigate here
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);

    // remove the loading screen from the backstack so the user doesn't see it again when hitting the back button
    NavigationService.RemoveBackEntry();
}

我只是不明白他的意思:

var page = new PageToNavigateTo();

我应该在那里做什么?我的意思是,我必须调用哪种方法来创建我想要导航到的页面的新实例? 另外,在NavigateToNextScreen()方法中,我是否必须使用通常的

NavigationService.Navigate(new Uri("/CreditiInfo.xaml", UriKind.Relative));

还是其他什么? 你能帮我完成这段代码吗? :)

谢谢!

塞尔吉奥

1 个答案:

答案 0 :(得分:0)

我建议'PageToNavigateTo()'应该是下一页名称的页面构造函数。所以在你的情况下你会有:

var page = new MainPage();

用于导航。这是唯一的方法,我知道在Windows Phone 8应用程序的页面之间导航。

相关问题