[英]Transferring data from one page to another windows 8 phone app
I'm trying to transfer data from one page to another with a windows phone app I'm creating. 我正在尝试使用正在创建的Windows Phone应用将数据从一页转移到另一页。 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? 我收到错误消息错误1当前上下文中不存在名称'enterNameBox'有人可以告诉我我做错了什么吗? First Page(getStartedButton takes you to other page) [code] 第一页(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] [/码]
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] [/码]
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 在此处可以找到更多信息如何为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. 当您按下“后退”按钮时,“单选”按钮也会自动更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.