![](/img/trans.png)
[英]Windows store app WInRT XAML C# - How to get navigated page textbox value
[英]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值
除了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
更多详细信息,请参考官方示例 。
另一种方法是将文本临时保存在infoPage
ApplicationData.LocalSettings中 , infoPage
在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
写入本地文件,并将数据库中保存的数据绑定到MainPage
。 有关uwp中MVVM的更多详细信息,您可以参考本文 。
最简单的方法是将值从InfoPage存储到App对象内的全局变量,然后在MainPage中检索它。
在app.xaml.cs中,定义一个字符串或列表,
public string commonValue;
在信息页中
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBox Name="tb1" Text="Hello"/>
在后面的InfoPage代码中,我们将文本框的值存储到应用程序中。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.