除了 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。