如何从Window Phone 7应用程序中检索数据

时间:2011-12-08 20:19:20

标签: windows-phone-7

我需要一些帮助。我的应用程序中有2页。页面#1有3个按钮。

   Button #1 = the name is "amount" and the content is "blank".

   Button #2 = the name is "tips" and the content is "blank".

   Button #3 = the name is "split" and the content is "blank".

当我点击任何按钮时,该应用程序会导航到第2页。 在第2页中,我想输入一些值,将值存储到IsolatedStorageSettings中,然后在Page#1中检索它,并在按下的按钮内容中显示该值。

Ex:按下按钮#3。在页面#2中,我输入一些值并将值存储在IsolatedStorageSettings中的“SplitAmount”文件中。现在在页面#1中,我想要检索该值并将其显示为按钮#3的内容。

问题:如何让应用程序知道按下了哪个按钮,这样我就可以将值存储到IsolatedStorageSettings中的正确文件中而无需为每个按钮创建页面?

PS:我希望我能够清楚地解释自己,加上我还是个菜鸟。放轻松吧。

2 个答案:

答案 0 :(得分:2)

我希望这会有所帮助。 :)

Navigation in depth

你必须看看在页面部分之间传递数据。

答案 1 :(得分:0)

创建一个Click事件处理程序并将其分配给所有三个按钮。然后将单击按钮的名称作为参数传递给第2页。

点击处理程序如下所示:

private void button_Click(object sender, RoutedEventArgs e)
{
    // get button name from sender - this can be button #1, #2 or #3
    string buttonName = ((Button)sender).Name;
    // craft Uri string so that it contains buttonName
    NavigationService.Navigate(new Uri("/Page2.xaml?buttonName=" + buttonName, UriKind.Relative));
}

将此处理程序分配给所有三个按钮的Click事件。您可以使用Visual Studio的“属性”窗口执行此操作。查看代码处理程序如何从sender动态获取按钮名称,并将其作为参数buttonName附加到Uri字符串。

当用户点击任何按钮时,调用处理程序并导航到Page2.xaml。假设我们想在页面打开时访问buttonName权限。为此,您可以覆盖OnNavigatedTo方法:

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

    if (NavigationContext.QueryString.ContainsKey("buttonName"))
        MessageBox.Show("The user pressed button " + NavigationContext.QueryString["buttonName"]);
}

可以通过QueryString字典访问传递的参数。代码检查条目buttonName是否存在。如果是,则显示一个消息框。

当然,您可以在以后获取buttonName,无需覆盖OnNavigatedTo。当您将数据保存到隔离存储时,您可能会稍后阅读它。

相关问题