Tri*_*era 0 xaml c#-4.0 win-universal-app uwp uwp-xaml
在我的页面右侧有一些文本框(第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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public MainPage()
{
this.InitializeComponent();
this.frame1.Navigate(typeof(MenuPage));
this.frame2.Navigate(typeof(Page1));
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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();
}
Run Code Online (Sandbox Code Playgroud)
第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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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!";
}
Run Code Online (Sandbox Code Playgroud)
更新:MainPage调用第1页:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Navigate to Page 1" Click="Button_Click" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
代码背后:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Page1));
}
Run Code Online (Sandbox Code Playgroud)
第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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
private void tb_GotFocus(object sender, RoutedEventArgs e)
{
myframe.Navigate(typeof(Page2));
}
Run Code Online (Sandbox Code Playgroud)
第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>
Run Code Online (Sandbox Code Playgroud)
代码背后有点不同:
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!";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2981 次 |
| 最近记录: |