【问题标题】:Windows store app WInRT XAML C# - How to get navigated page textbox valueWindows 商店应用程序 WInRT XAML C# - 如何获取导航页面文本框值
【发布时间】:2017-02-20 17:45:09
【问题描述】:

Windows 商店应用程序 WInRT XAML C# - 如何获取导航页面文本框值。这是我的所有代码.......................... ......................

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
    }

【问题讨论】:

    标签: c# xaml windows-store-apps winrt-xaml


    【解决方案1】:

    足够简单的问题。试试这个:

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

    这样就可以了。

    【讨论】:

      【解决方案2】:

      有几种方法可以实现您的要求。如果您使用的是 MVVM 模式(我个人强烈推荐),那么您只需将 TextBox.Text 绑定到 ViewModel 中的一个字段,并对 MainPage 中的同一个 ViewModel 执行相同操作...(这可以讨论更深入,所以让我知道这是否是你正在做的。)如果你没有使用 MVVM 模式,那么你可能不知道我在说什么,所以让我们看看其他选项。

      另一种可能的解决方案是对新页面进行接口,要求它们都具有相同的接口,从而公开 TextBox.Text 属性或具有相同名称的 TextBox 或您决定的名称。这是更深层次的,因为它可能依赖于其他内部功能,例如依赖注入和/或控制器,具体取决于您的设置方式。

      如果您仍然不知道我在说什么,那么我将假设您是半新手,并建议您通过不同的方法简单地查找 TextBox。如果您至少提供了两个页面的完整代码和 XAML,那么直接回答问题会容易得多。

      【讨论】:

      • MainPage.Xml :
      • MainPage :
      • 请给我发你的电子邮件..我是新来这里的人在这里评论问题
      • @Aizaz Ahmad 您可以通过在其中添加代码来编辑您的问题,这样我们可以更好地帮助您,谢谢!
      • @Cherry Bu i 编辑了我的问题...编辑起来非常麻烦.. (^-^)
      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多