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

Trishit贝拉

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

格蕾丝·冯

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

例如: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!";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

通过单击按钮不正确,其他文本框中的文本框值

来自分类Dev

当使用JavaScript中的单个功能单击其他单选按钮时,如何禁用文本框?

来自分类Dev

转到其他页面后如何在文本框中保留值?

来自分类Dev

如何使用其他文本框的值设置文本框的值

来自分类Dev

如何在MVC的单选按钮列表中明确包含“其他”文本框?

来自分类Dev

单击“编辑”按钮时如何在gridview中获取文本框值?

来自分类Dev

如何在页面init的文本框中设置光标

来自分类Dev

如何在文本框值中输入当前日期并显示我的其他数据

来自分类Dev

如何在Laravel 4中基于其他文本框隐藏/显示文本框?

来自分类Dev

在特定页面上以及文本框是否具有值时如何隐藏按钮

来自分类Dev

单击表单提交以外的按钮后,如何在jsp页面的会话中添加一些文本框值?

来自分类Dev

如何通过单击UserControl中的按钮将UserControl中存在的文本框值传递给Aspx页面标签

来自分类Dev

如何通过单击UserControl中的按钮将UserControl中存在的文本框值传递给Aspx页面标签

来自分类Dev

如何在uwp应用程序中将Listview项目用作按钮?或者如何通过单击uwp中的列表视图项来更改文本框的文本?

来自分类Dev

导航到其他页面后如何保留文本框值?

来自分类Dev

如何在ASP页面加载时通过文本框中的用户给定值创建动态链接按钮?

来自分类Dev

单击提交按钮后如何保留文本框值

来自分类Dev

如何获得单击按钮上的文本框值?

来自分类Dev

如何通过按钮单击Ajax获取文本框值

来自分类Dev

如何在UWP的文本框中添加2个按钮?

来自分类Dev

每次用户单击Windows Phone中的按钮时,如何增加文本框的值?

来自分类Dev

单击按钮时如何在文本框中显示不同的sql数据

来自分类Dev

单击相应按钮时如何在文本框中附加特殊符号

来自分类Dev

如何在WPF中单击按钮更新文本框背景色

来自分类Dev

单击kendo按钮时如何在div中添加新的文本框?

来自分类Dev

为什么从其他文本框中删除值后仍然禁用文本框

来自分类Dev

如何设置从servlet返回的值到JSP页面中的文本框

来自分类Dev

如何设置从servlet返回的值到JSP页面中的文本框

Related 相关文章

  1. 1

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

  2. 2

    通过单击按钮不正确,其他文本框中的文本框值

  3. 3

    当使用JavaScript中的单个功能单击其他单选按钮时,如何禁用文本框?

  4. 4

    转到其他页面后如何在文本框中保留值?

  5. 5

    如何使用其他文本框的值设置文本框的值

  6. 6

    如何在MVC的单选按钮列表中明确包含“其他”文本框?

  7. 7

    单击“编辑”按钮时如何在gridview中获取文本框值?

  8. 8

    如何在页面init的文本框中设置光标

  9. 9

    如何在文本框值中输入当前日期并显示我的其他数据

  10. 10

    如何在Laravel 4中基于其他文本框隐藏/显示文本框?

  11. 11

    在特定页面上以及文本框是否具有值时如何隐藏按钮

  12. 12

    单击表单提交以外的按钮后,如何在jsp页面的会话中添加一些文本框值?

  13. 13

    如何通过单击UserControl中的按钮将UserControl中存在的文本框值传递给Aspx页面标签

  14. 14

    如何通过单击UserControl中的按钮将UserControl中存在的文本框值传递给Aspx页面标签

  15. 15

    如何在uwp应用程序中将Listview项目用作按钮?或者如何通过单击uwp中的列表视图项来更改文本框的文本?

  16. 16

    导航到其他页面后如何保留文本框值?

  17. 17

    如何在ASP页面加载时通过文本框中的用户给定值创建动态链接按钮?

  18. 18

    单击提交按钮后如何保留文本框值

  19. 19

    如何获得单击按钮上的文本框值?

  20. 20

    如何通过按钮单击Ajax获取文本框值

  21. 21

    如何在UWP的文本框中添加2个按钮?

  22. 22

    每次用户单击Windows Phone中的按钮时,如何增加文本框的值?

  23. 23

    单击按钮时如何在文本框中显示不同的sql数据

  24. 24

    单击相应按钮时如何在文本框中附加特殊符号

  25. 25

    如何在WPF中单击按钮更新文本框背景色

  26. 26

    单击kendo按钮时如何在div中添加新的文本框?

  27. 27

    为什么从其他文本框中删除值后仍然禁用文本框

  28. 28

    如何设置从servlet返回的值到JSP页面中的文本框

  29. 29

    如何设置从servlet返回的值到JSP页面中的文本框

热门标签

归档