如何在uwp中的其他页面上单击按钮的文本框中设置值?

how to set value in text box on button click on other page in uwp?

在我的页面右侧有一些文本框(第 1 页),在左侧有一些按钮,如文本框上的 1、2、3(第 2 页)得到焦点,第 2 页出现在框架中。 现在我的问题是如何在 uwp 中第 2 页上的按钮单击时在第 1 页中的文本框中设置值。

您可以使用 FrameworkElement.FindName method 从第 2 页获取第 1 页中的 TextBox

例如这里: MainPage 有两个 Frame:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <Frame x:Name="frame1"></Frame>
    <Frame x:Name="frame2" Grid.Column="1"></Frame>
</Grid>

隐藏代码:

public MainPage()
{
    this.InitializeComponent();
    this.frame1.Navigate(typeof(MenuPage));
    this.frame2.Navigate(typeof(Page1));
}

MenuPage 是我测试中的空白页。

第 1 页:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Tapped="Grid_Tapped">
    <TextBlock Name="textBlock" Text="This is Page 1" VerticalAlignment="Top" FontSize="25" />
    <Button VerticalAlignment="Top" Width="1"></Button>
    <StackPanel VerticalAlignment="Center">
        <TextBox Name="tb1" GotFocus="tb_GotFocus" />
        <TextBox Name="tb2" Margin="0,30" GotFocus="tb_GotFocus" />
        <TextBox Name="tb3" GotFocus="tb_GotFocus" />
    </StackPanel>
</Grid>

隐藏代码:

public Page1()
{
    this.InitializeComponent();
    this.Loaded += Page1_Loaded;
}

private Frame frame;

private void Page1_Loaded(object sender, RoutedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    Page mainPage = rootFrame.Content as MainPage;
    frame = mainPage.FindName("frame1") as Frame;
}

private void tb_GotFocus(object sender, RoutedEventArgs e)
{
    frame.Navigate(typeof(Page2));
}

private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
{
    if (frame.CanGoBack)
        frame.GoBack();
}

第 2 页:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="This is Page 2" VerticalAlignment="Top" FontSize="25" />
    <StackPanel VerticalAlignment="Center">
        <Button Content="Button 1" Click="Button_Click_1" />
        <Button Content="Button 2" Click="Button_Click_2" Margin="0,30" />
        <Button Content="Button 3" Click="Button_Click_3" />
    </StackPanel>
</Grid>

隐藏代码:

public Page2()
{
    this.InitializeComponent();
    this.Loaded += Page2_Loaded;
}

private TextBox tb1;
private TextBox tb2;
private TextBox tb3;

private void Page2_Loaded(object sender, RoutedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    Page mainPage = rootFrame.Content as MainPage;
    Frame frame = mainPage.FindName("frame2") as Frame;
    Page page1 = frame.Content as Page1;
    tb1 = page1.FindName("tb1") as TextBox;
    tb2 = page1.FindName("tb2") as TextBox;
    tb3 = page1.FindName("tb3") as TextBox;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    tb1.Text = "Button 1 Clicked!";
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    tb2.Text = "Button 2 Clicked!";
}

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    tb3.Text = "Button 3 Clicked!";
}

渲染图像:

更新: MainPage 调用第 1 页:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Navigate to Page 1" Click="Button_Click" />
</Grid>

隐藏代码:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Frame.Navigate(typeof(Page1));
}

第 1 页有框架并调用第 2 页:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <Frame Name="myframe"></Frame>
    <Grid Grid.Column="1">
        <TextBlock Name="textBlock" Text="This is Page 1" VerticalAlignment="Top" FontSize="25" />
        <Button VerticalAlignment="Top" Width="1"></Button>
        <StackPanel VerticalAlignment="Center">
            <TextBox Name="tb1" GotFocus="tb_GotFocus" />
            <TextBox Name="tb2" Margin="0,30" GotFocus="tb_GotFocus" />
            <TextBox Name="tb3" GotFocus="tb_GotFocus" />
        </StackPanel>
    </Grid>
</Grid>

隐藏代码:

private void tb_GotFocus(object sender, RoutedEventArgs e)
{
    myframe.Navigate(typeof(Page2));
}

第 2 页:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="This is Page 2" VerticalAlignment="Top" FontSize="25" />
    <StackPanel VerticalAlignment="Center">
        <Button Content="Button 1" Click="Button_Click_1" />
        <Button Content="Button 2" Click="Button_Click_2" Margin="0,30" />
        <Button Content="Button 3" Click="Button_Click_3" />
    </StackPanel>
</Grid>

后面的代码有点不同:

public Page2()
{
    this.InitializeComponent();
    this.Loaded += Page2_Loaded;
}

private TextBox tb1;
private TextBox tb2;
private TextBox tb3;

private void Page2_Loaded(object sender, RoutedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    Page page1 = rootFrame.Content as Page1;
    tb1 = page1.FindName("tb1") as TextBox;
    tb2 = page1.FindName("tb2") as TextBox;
    tb3 = page1.FindName("tb3") as TextBox;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    tb1.Text = "Button 1 Clicked!";
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    tb2.Text = "Button 2 Clicked!";
}

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    tb3.Text = "Button 3 Clicked!";
}

这是我找到的解决方案。它是上述部分的组合。 ShellPage 是一个框架页面。它有一个文本块,此代码在 app.xaml.cs 页面中。

ShellPage currentPage = Window.Current.Content as ShellPage;
TextBlock tb1 = currentPage.FindName("InternetConnection") as TextBlock;
tb1.Text = InternetConnectionText;
tb1.Foreground = InternetConnectionForeground;