2011-11-22 76 views
1

正如您所看到的,我想导航到“ScoreInputDialog.xaml”页面,用户可以在其中键入一个名称。在此之后,我试图将名称保存到列表中,但它总是空的,因为最终导航到页面“ScoreInputDialog.xaml”正在完成。在继续执行其他代码之前,如何导航到期望的页面并获取我的价值?为什么NavigationService.Navigate只在最后运行?

NavigationService.Navigate(new Uri("/ScoreInputDialog.xaml", UriKind.Relative)); // Sets tempPlayerName through a textbox. 
if (phoneAppService.State.ContainsKey("tmpPlayerName")) 
{ 
    object pName; 
    if (phoneAppService.State.TryGetValue("tmpPlayerName", out pName)) 
    { 
     tempPlayerName = (string)pName; 
    } 
} 
highScorePlayerList.Add(tempPlayerName); 

回答

2

您应该Navigate电话后直接做任何事。相反,覆盖你是从,得到通知来的页面OnNavigatedTo方法,当用户回来

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 

当用户按下退出“ScoreInputDialog.xaml”,或许这个方法会被调用后退按钮或因为您致电NavigationService.GoBack()。这将退出“ScoreInputDialog.xaml”页面并转到上一页,在那里将调用OnNavigatedTo。这是检查价值的时间。

插图导航流量:

“OriginPage” --- [Navigate] ---> “ScoreInputDialog” --- [GoBack()或后退按钮] ---> “OriginPage”(*)

(*)在那里将调用OnNavigatedTo。实施看起来是这样的:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    if (phoneAppService.State.ContainsKey("tmpPlayerName")) 
    { 
     object pName; 
     if (phoneAppService.State.TryGetValue("tmpPlayerName", out pName)) 
     { 
      tempPlayerName = (string)pName; 
     } 
     highScorePlayerList.Add(tempPlayerName); 
    } 
} 

记住调用Navigate之前清除临时球员的名字:

phoneAppService.State.Remove("tmpPlayerName"); 
NavigationService.Navigate(new Uri("/ScoreInputDialog.xaml", UriKind.Relative)); 

注:OnNavigatedTo也将在用户看到的页面在第一时间或导航叫从“ScoreInputDialog.xaml”以外的页面返回。但是,那么“tmpPlayerName”值将不会被设置。

+0

感谢您的示例和解释。它解决了我的问题。 – Mudasar

相关问题