【问题标题】:Windows Store App XAML - How to get textbox value of navigated pageWindows Store App XAML - 如何获取导航页面的文本框值
【发布时间】:2017-02-27 20:57:14
【问题描述】:

Windows Store App XAML - 如何获取导航页面的文本框值。我有 2 个页面 1.MainPage.xaml 2.Infopage.xaml 在 MainPage 中,我有一个按钮(用于获取 InfoPage 的 TextBox 值)和一个框架(用于导航 InfoPage).. 在 InfoPage 中有一些 TextBoxes..现在我怎样才能获得 InfoPage TextBox 值

【问题讨论】:

    标签: c# xaml windows-store-apps windows-10-universal windows-store


    【解决方案1】:

    除了 Neal 的解决方案之外,还有另外两种方法可供您参考。

    一种方法是在infoPage 上定义一个静态参数并将值设置为当前页面。然后您可以从MainPage 调用infoPage 上的方法。代码如下:

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

    更多关于ApplicationData的详情请参考official sample

    另一种方法是将文本临时保存在infoPage 上的ApplicationData.LocalSettings 中,然后在MainPage 上读出文本。代码如下:

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

    如果你有大量数据,更好的方法是创建一个本地文件作为数据库,并使用MVVM模式将数据从infoPage写入本地文件,并将保存在数据库中的数据绑定到@987654339 @。更多关于 uwp 中 MVVM 的详细信息,您可以参考this article

    【讨论】:

    • 哇!通过执行此第一页代码解决了我的问题: public static FormFillPersonalinfo Current;公共 FormFillPersonalinfo() { this.InitializeComponent();当前=这个; } public string ffptitle() { return (Title.SelectedItem as FlipViewItem).Content.ToString(); } public string ffpfirstname() { return FirstName.Text; }
    • 第二页代码:private void AppBarButton_Click(object sender, RoutedEventArgs e) { mm.Text = FormFillPersonalinfo.Current.ffpbirthday(); }
    【解决方案2】:

    最简单的方法是将 InfoPage 中的值存储到 App 对象内的全局变量中,然后在 MainPage 中检索它。

    在app.xaml.cs中,定义一个字符串或List,

    public string commonValue;
    

    在信息页中

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

    在后面的 InfoPage 代码中,我们将 textbox 的值存储到应用程序中。

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

    然后在MainPage中:

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

    在后面的 MainPage 代码中,我们需要初始化 InfoPage 然后取值:

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

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多