Aizaz Ahmad
Aizaz Ahmad

Reputation: 23

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

Upvotes: 0

Views: 1998

Answers (2)

Sunteen Wu
Sunteen Wu

Reputation: 10627

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 .

Upvotes: 1

Neal wang - MSFT
Neal wang - MSFT

Reputation: 323

The easiest way is to store the value from the InfoPage to the global variable inside the App object and then retrieve it in the MainPage.

In the app.xaml.cs, define a string or List,

public string commonValue;

In the InfoPage

    <StackPanel Orientation="Vertical" VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBox Name="tb1" Text="Hello"/>

In the InfoPage code behind, we store the value of textbox to the app.

        public InfoPage()
    {
        this.InitializeComponent();
        App app = Application.Current as App;
        app.commonValue = tb1.Text;
    }

Then in the MainPage:

    <StackPanel VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="MainPage" Click="Button_Click"/>
    <TextBox Name="textbox1"/>

In the MainPage code behind, we need to initialize the InfoPage then retrieve the value:

    public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        InfoPage info = new InfoPage();
        info.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        App app = Application.Current as App;
        textbox1.Text = app.commonValue;
    }
}

Upvotes: 0

Related Questions