gpt4 book ai didi

c# - 为什么 NavigationService.Navigate 只在最后运行?

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:33 25 4
gpt4 key购买 nike

如您所见,我想导航到“ScoreInputDialog.xaml”页面,用户可以在其中键入名称。在此之后,我试图将名称保存到列表中,但它始终为空,因为导航到页面“ScoreInputDialog.xaml”终于完成了。在继续其余代码之前,如何导航到所需页面并获取我的值?

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);

最佳答案

Navigate 调用之后您不应该直接做任何事情。而是覆盖您来自的页面的 OnNavigatedTo 方法,以便在用户回来时得到通知:

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

此方法将在用户退出“ScoreInputDialog.xaml”时调用,可能是通过按下后退按钮或因为您调用了 NavigationService.GoBack()。这将退出“ScoreInputDialog.xaml”页面并转到上一页,将调用 OnNavigatedTo。这是检查值的时间。

导航流程说明:

"OriginPage"---[Navigate]---> "ScoreInputDialog"---[GoBack() or Back-button]---> "来源页面"(*)

在 (*) 所在的位置 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”值。

关于c# - 为什么 NavigationService.Navigate 只在最后运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8234333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com