我尝试使用我正在创建的Windows手机应用程序将数据从一个页面传输到另一个页面。我收到错误错误1名称' enterNameBox'在当前上下文中不存在 谁能告诉我我做错了什么? 第一页(getStartedButton带你到其他页面) [代码]
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]
第二页 [代码]
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"];
}
}
}
[/代码]
答案 0 :(得分:2)
我建议使用电话应用程序服务来保存页面之间的值。
PhoneApplicationService.Current.State["id_of_value"] = value;
第一页
PhoneApplicationService.Current.State["enterNameBox"] = "test"
第二页
String my_string = (String) PhoneApplicationService.Current.State["enterNameBox"];
可在此处找到更多信息How to preserve and restore app state for Windows Phone 8
编辑: 正在回答你的单选按钮评论,但看起来你删除了它。它适用于大多数类型。
第一页
PhoneApplicationService.Current.State["radio_button1"] = this.your_radio_button;
第二页
RadioButton rb = (RadioButton)PhoneApplicationService.Current.State["radio_button1"];
rb.IsChecked = true; // or false
当您按“返回”按钮时,“单选”按钮也会自动更新。