我想从TextBox中读取文本,然后将其发送到另一个页面。但是在另一个页面上,我一直得到空字符串。为什么这个不起作用?
我在第一页上有这个:
public string _beseda()
{
    return textBox1.Text;          
}在第2页,我应该检索这个字符串:
private void button1_Click(object sender, RoutedEventArgs e)
{
    Page2 neki = new Page2();
    MessageBox.Show(neki._beseda());
}发布于 2013-04-06 06:00:20
在windows phone中,有两种在页面之间传递数据的策略。
1.使用App.cs
打开App.xaml背后的App.cs代码写道:
 // To store Textbox the value   
 public string storeValue;在Page1中
 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        App app = Application.Current  as App;
        app.storeValue = textBox1.Text;            
    }在Page2上
 private void button1_Click(object sender, RoutedEventArgs e) {
    App app = Application.Current  as App;
    MessageBox.Show(app.storeValue);
}2.在导航时将值作为参数传递
在导航将文本框值嵌入到页面Url之前
    string newUrl = "/Page2.xaml?text="+textBox1.Text;
    NavigationService.Navigate(new Uri(newUrl, UriKind.Relative));在Page2中
    //Temporarily hold the value got from the navigation 
    string textBoxValue = "";
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        //Retrieve the value passed during page navigation
         NavigationContext.QueryString.TryGetValue("text", out textBoxValue)               
    }
     private void button1_Click(object sender, RoutedEventArgs e) {
       MessageBox.Show(textBoxValue);
     }下面是一些有用的链接。
发布于 2013-04-06 04:44:26
这里面有很多问题。您说您在Page1上有_beseda()函数,但是您在button1_click()中引用了Page2()。此外,如果我假设您在button1_click()中指的是Page1,那么您正在创建新的Page1,然后向它请求文本框的文本……所以它当然是空的。你什么都没放进去。
即使您打算将Page2放在那里,问题仍然是一样的。
https://stackoverflow.com/questions/15843135
复制相似问题