2013-04-05 40 views
0

我想從TextBox中讀取文本,然後將其發送到另一頁。但在另一頁我保持空串。 爲什麼這不起作用?從文本框中讀取並傳遞到另一頁

我有這個第1頁:

public string _beseda() 
{ 
    return textBox1.Text;   
} 

和第2頁上,我應該找回這個字符串:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    Page2 neki = new Page2(); 
    MessageBox.Show(neki._beseda()); 
} 

回答

1

在windows phone的頁面之間傳遞數據有兩種策略。

  1. 導航

1.使用App.cs

使用時的App.cs

  • 通行證數據作爲參數值打開App.cs代碼App.xaml中寫入的背後:

    // To store Textbox the value 
    public string storeValue; 
    

    In 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.傳值作爲參數

    在導航

    在第二頁

    嵌入文本框的值導航到網頁的網址

    string newUrl = "/Page2.xaml?text="+textBox1.Text; 
        NavigationService.Navigate(new Uri(newUrl, UriKind.Relative)); 
    

    之前

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

    下面是一些有用的鏈接..

  • +0

    感謝這幫助了很多,我知道這是一種基本的Q,但我是新來的這,所以再次感謝。 – Veleje 2013-04-06 21:30:25

    +0

    不客氣,很高興有幫助。 – 2013-04-17 10:54:08

    1

    有一些問題。你說你在Page1上有_beseda()功能,但是你在button1_click()中參考Page2()。此外,如果我假設你的意思是,你正在創建新的Page1然後你問它的文本框的文本....所以當然是空的。你沒有放入任何東西。

    即使你打算把Page2代替,問題仍然是一樣的。

    相關問題