如何在UWP中获取另一个页面的值

问题描述 投票:3回答:2

我不知道如何开始,所以我只是从截图及其代码开始:

The Textbox.Text-Value should also be in the Textblock at the Bottom here


MainPage.xaml中

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="48"/>
    </Grid.RowDefinitions>

    <Frame x:Name="MainContent"/>

    <Grid Grid.Row="1" Background="Gray">
        <TextBlock x:Name="ResultTB"
                   VerticalAlignment="Center"
                   Text="Should also be here"
                   HorizontalAlignment="Center"/>
    </Grid>

</Grid>

MainPage.xaml.cs中

public MainPage()
    {
        InitializeComponent();
        MainContent.Navigate(typeof(ContentFrame));
    }

ContentFrame.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBox x:Name="Input"
             VerticalAlignment="Center"
             HorizontalAlignment="Center"
             Width="200"/>
</Grid>

现在我想研究ResultTB的Input文本。这样做最简单,最好的方法是什么?

c# xaml win-universal-app
2个回答
0
投票

您可以使用Navigate方法的覆盖:Frame.Navigate(TypeName, Object)

其中Object是参数。您可以在OnNavigatedTo事件中进入另一个页面。就像是:

string txt=ResultTB.Text;
MainContent.Navigate(typeof(ContentFrame), txt);  

并在另一个页面上添加OnNavigatedTo事件和内部:

var parameter = e.Parameter as string;

0
投票

对于未来寻找此问题的人 - 对Alexej的一个类似答案但更简单:例如,假设我要更改MainPage中对象“SomeObject”(x:Name =“SomeObject”)的文本:

MainPage mainFrame = (MainPage)((Frame)Window.Current.Content).Content;
mainFrame.SomeObject = "my text";
© www.soinside.com 2019 - 2024. All rights reserved.