[英]Windows Store App XAML - How to get textbox value of navigated page
Windows Store App XAML-如何获取导航页面的文本框值。我有2个页面1.MainPage.xaml 2.MainPage中的Infopage.xaml我有一个Button(获取InfoPage的TextBox值)和一个框架(导航InfoPage) )..在InfoPage中有一些TextBoxes..now如何获取InfoPage TextBox值
In additional to Neal's solution, here is another two ways you can also reference. 除了Neal的解决方案之外,您还可以参考以下两种方法。
One way is to define a static parameter on infoPage
and set the value to current page. 一种方法是在
infoPage
上定义静态参数并将其值设置为当前页面。 Then you can invoke the method on infoPage
from MainPage
. 然后,您可以从
MainPage
调用infoPage
上的方法。 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 . 有关
ApplicationData
更多详细信息,请参考官方示例 。
Another way is to save the text temporary in ApplicationData.LocalSettings on infoPage
and read the text out on the MainPage
. 另一种方法是将文本临时保存在
infoPage
ApplicationData.LocalSettings中 , infoPage
在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
. 如果您有大量数据,更好的方法是将本地文件创建为数据库,并使用MVVM模式将数据从
infoPage
写入本地文件,并将数据库中保存的数据绑定到MainPage
。 More details about MVVM in uwp you can reference this article . 有关uwp中MVVM的更多详细信息,您可以参考本文 。
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. 最简单的方法是将值从InfoPage存储到App对象内的全局变量,然后在MainPage中检索它。
In the app.xaml.cs, define a string or List, 在app.xaml.cs中,定义一个字符串或列表,
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. 在后面的InfoPage代码中,我们将文本框的值存储到应用程序中。
public InfoPage()
{
this.InitializeComponent();
App app = Application.Current as App;
app.commonValue = tb1.Text;
}
Then in the MainPage: 然后在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: 在后面的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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.