在“我的页面”右侧,有一些文本框(第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] 删除。
我来说两句