简体   繁体   English

Windows Store App XAML-如何获取导航页面的文本框值

[英]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中infoPageMainPage上读出文本。 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.

相关问题 Windows商店应用程序WInRT XAML C# - 如何获取导航的页面文本框值 - Windows store app WInRT XAML C# - How to get navigated page textbox value 如何将文本框的文本获取到另一个 xaml 中的文本块? c# windows store app - How to get the text of textbox to textblock in another xaml? c# windows store app 为Windows 8应用商店应用创建自定义XAML文本框 - Creating a Custom XAML TextBox for Windows 8 Store app C#XAML Windows应用商店中带有文本框的InputDialog / MessageDialog - InputDialog/MessageDialog with a textbox in C# XAML Windows store app Windows应用商店应用 - 如何在搜索超级按钮文本框中获取水印 - Windows Store App - how to get watermark in Search Charm textbox 如何从XAML中的文本框中获取值? - How to get the value out of a textbox in XAML? 如何从页面中的App.xaml获取颜色值 - How to Get a Color Value from App.xaml in a Page Windows应用商店-XAML-动画ListView详细信息(拆分页面) - Windows Store App - XAML - Animate ListView Details (Split Page) Windows应用商店-XAML-边框填充页面,而不是包装网格 - Windows Store App - XAML - Border filling page rather then wrapping Grid Windows应用商店-XAML C#-如何在代码中访问Page.Resource - Windows Store App - XAML C# - How to access Page.Resource in code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM