【发布时间】:2014-07-08 07:09:28
【问题描述】:
我正在为 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));
还是别的什么? 你能帮我完成这段代码吗? :)
谢谢!
塞尔吉奥
【问题讨论】:
标签: c# xaml windows-phone-8