Search code examples
c#xamlwindows-store-appswinrt-xaml

Windows store app WInRT XAML C# - How to get navigated page textbox value


Windows store app WInRT XAML C# - How to get navigated page textbox value.here is my all code...................................................

1.MainPage.xaml

    <Button Content="Navigate" Name="nav" HorizontalAlignment="Left" Margin="826,198,0,0" VerticalAlignment="Top" Click="nav_Click"/>
    <TextBlock HorizontalAlignment="Left" Margin="402,201,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="27" Width="155"/>
    <Frame Name="framee"  VerticalAlignment="Top" Height="440" HorizontalAlignment="Left" Width="600" Margin="402,238,0,0"/>
    <Button Content="Get" Name="get" HorizontalAlignment="Left" Margin="1062,193,0,0" VerticalAlignment="Top" Click="get_Click"/>

2.InfoPage.xaml

<TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Name="Name" VerticalAlignment="Top" Margin="0,6,0,0" Width="348"/>
<TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Name="Address" VerticalAlignment="Top" Margin="0,43,0,0" Width="348"/>
<TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Name="Phone" VerticalAlignment="Top" Margin="0,80,0,0" Width="348"/>

MainPage.cs

 private void nav_Click(object sender, RoutedEventArgs e)
    {
        framee.Navigate(typeof(InfoPage));
    }

    private void get_Click(object sender, RoutedEventArgs e)
    {
      here textbox name are not showing
    }

Solution

  • Simple enough question. Try this:

    MainPage.xaml

    <TextBox x:Name="MyTextBox" Text="Hello" />
    <Button Click="Button_Click" />
    

    MainPage.xaml.cs

    void Button_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(InfoPage), MyTextBox.Text);
    }
    

    InfoPage.xaml

    <TextBlock x:Name="MyTextBlock" />
    

    InfoPage.xaml.cs

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        MyTextBlock.Text = e.Parameter?.ToString();
    }
    

    That will do it.