chr.solr
chr.solr

Reputation: 576

How can I retrieving data from my Window Phone 7 App

I need some help. I have a 2 pages in my app. Page #1 with 3 buttons.

   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".

When I click in any of the buttons, the app navigates to page #2. In page #2, I want to enter some values, store the value into IsolatedStorageSettings, then retrive it in Page #1 and display the value in the content of the button that was pressed.

Ex: Button #3 was pressed. In Page #2 I enter some values and store the value to "SplitAmount" file in IsolatedStorageSettings. Now in Page #1 I want to retrieve the value and display it as the content for the button #3.

Question: How can I make the app knows which button was pressed, so I can store the value to the right file in IsolatedStorageSettings without the need to create a page for each button?

PS: I hope I explain myself clear enough, plus I'm still a noob. Take it easy on me.

Upvotes: 0

Views: 72

Answers (2)

Heinrich Ulbricht
Heinrich Ulbricht

Reputation: 10372

Create one Click event handler and assign it to all three buttons. Then pass the name of the clicked button as parameter to page #2.

The click handler looks like this:

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

Assign this handler to the Click event of all three buttons. You can use the Properties window of Visual Studio to do this. Look at the code how the handler dynamically gets the button name from its sender and appends it to the Uri string as parameter buttonName.

When the user clicks any of the buttons the handler is invoked and navigates to Page2.xaml. Assume we want to access the buttonName right when the page opens. To do this you override the OnNavigatedTo method:

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"]);
}

The passed parameter can be accessed via the QueryString dictionary. The code checks if the entry buttonName exists. If it does it displays a message box.

Of course you can get your buttonName at any later time, no need to override OnNavigatedTo. You probably would read it later when you save data to isolated storage.

Upvotes: 0

BigL
BigL

Reputation: 1631

I hope this will help. :)

Navigation in depth

And you will have to take a look at passing data between pages part.

Upvotes: 2

Related Questions