Aizaz Ahmad Aizaz Ahmad - 1 month ago 7
C# Question

Windows Store App XAML - How to get textbox value of navigated page

Windows Store App XAML - How to get textbox value of navigated page.i have 2 pages
1.MainPage.xaml
2.Infopage.xaml
in the MainPage i have a Button(to get TextBox value of InfoPage) and a frame(to navigate InfoPage)..
in the InfoPage there are some TextBoxes..now how can i get InfoPage TextBox values

Answer

In additional to Neal’s solution, here is another two ways you can also reference.

One way is to define a static parameter on infoPage and set the value to current page. Then you can invoke the method on infoPage from MainPage. Code like follows:

infoPage

 public static InfoPage Current;
 public InfoPage()
 {
     this.InitializeComponent();
     Current = this;      
 }
public string gettext()
{
    return txttext.Text;
}

MainPage

private void btngetsecondpage_Click(object sender, RoutedEventArgs e)
{
    InfoPage infopage = InfoPage.Current;
    txtresult.Text = infopage.gettext();  
}

More details about ApplicationData please reference the official sample.

Another way is to save the text temporary in ApplicationData.LocalSettings on infoPage and read the text out on the MainPage. Code like follows:

infoPage

private void txttext_TextChanged(object sender, TextChangedEventArgs e)
{
    ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
    localSettings.Values["texboxtext"] =txttext.Text; // example value            
}

MainPage

 private void btngetsecondpage_Click(object sender, RoutedEventArgs e)
 { 
     ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
     if (localSettings.Values["texboxtext"] != null)
     {
         txtresult.Text = localSettings.Values["texboxtext"].ToString();
     }
 }

If you have a large amount of data, a better way is to create a local file as database, and use a MVVM pattern to write data into local file from infoPage and bind data which saved in the database to MainPage. More details about MVVM in uwp you can reference this article .

Comments