Reputation: 13
I'm trying to transfer data from one page to another with a windows phone app I'm creating. I'm getting the error Error 1 The name 'enterNameBox' does not exist in the current context Can someone tell me what I did wrong? First Page(getStartedButton takes you to other page) [code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using FinalProjectPhoneVersion.Resources;
namespace FinalProjectPhoneVersion
{
public partial class MainPage : PhoneApplicationPage
{
static int strikeCounter;
static int counter;
static Random random = new Random();
static int randomNumber1;
static int randomNumber2;
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
// Sample code for building a localized ApplicationBar
//private void BuildLocalizedApplicationBar()
//{
// // Set the page's ApplicationBar to a new instance of ApplicationBar.
// ApplicationBar = new ApplicationBar();
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
public MathPage()
{
InitializeComponent();
String welcomeString = (String)PhoneApplicationService.Current.State["enterNameBox"];
RadioButton easyMode = (RadioButton)PhoneApplicationService.Current.State["easyMode"];
RadioButton hardMode = (RadioButton)PhoneApplicationService.Current.State["hardMode"];
welcomeLabel.Text = "Welcome " + welcomeString;
if (easyMode.Checked = true) {
}
}
private void getStartedButton_Click(object sender, RoutedEventArgs e)
{
MathTime();
NavigationService.Navigate(new Uri("/Page1.xaml?enterNameBox=test", UriKind.Relative));
}
}
}
[/code]
Second Page [code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace FinalProjectPhoneVersion
{
public partial class MathPage : PhoneApplicationPage
{
public MathPage()
{
InitializeComponent();
welcomeLabel.Text = "Welcome " + enterNameBox.Text;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string parameterValue = NavigationContext.QueryString["enterNameBox"];
}
}
}
[/code]
Upvotes: 1
Views: 329
Reputation: 8161
I would suggest using Phone Application Service instead to save values between pages.
PhoneApplicationService.Current.State["id_of_value"] = value;
First page
PhoneApplicationService.Current.State["enterNameBox"] = "test"
Second page
String my_string = (String) PhoneApplicationService.Current.State["enterNameBox"];
More information can be found here How to preserve and restore app state for Windows Phone 8
Edit: Was in the middle of answering your radio button comment but looks like you deleted it. It should work for most types.
First Page
PhoneApplicationService.Current.State["radio_button1"] = this.your_radio_button;
Second Page
RadioButton rb = (RadioButton)PhoneApplicationService.Current.State["radio_button1"];
rb.IsChecked = true; // or false
When you press the Back button, your Radio button will update itself as well.
Upvotes: 2