如何在同一窗口中打开新的XAML页 [英] How to open a new XAML page in the same window
本文介绍了如何在同一窗口中打开新的XAML页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个XAML页面,当您单击其中一个按钮时,将打开另一个页面.我的代码是:
I have two XAML pages and when you click a button in one, the other page opens. My code is:
private void cookingButton(object sender, RoutedEventArgs e)
{
CookingMenu cooking = new CookingMenu();
cooking.Show();
}
CookingMenu是XAML页面的名称.此代码在新窗口中打开页面,但是有没有办法在同一窗口中打开页面?这两页的尺寸相同.
CookingMenu is the name of the XAML page. This code opens the page in a new window but is there any way to open the page in the same window? Both the pages are the same size.
推荐答案
希望这会有所帮助:
App.xaml.cs
App.xaml.cs
public partial class App : Application
{
public static MainWindow ParentWindowRef;
}
MainWindow.xaml
MainWindow.xaml
<Window x:Class="MultiplePageOpeninSameScreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MultiplePageOpeninSameScreen"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<DockPanel>
<Frame Name="ParentFrame"/>
</DockPanel>
</Window>
MainWindow.xaml.cs
MainWindow.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
App.ParentWindowRef = this;
this.ParentFrame.Navigate(new Page1());
}
Page1.xaml
Page1.xaml
<Page x:Class="MultiplePageOpeninSameScreen.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MultiplePageOpeninSameScreen"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Name="TxtBlockSomeTxt" Text="This is Page One" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Grid.Row="1" Name="BtnGoToPageTwo" Content="Go To Page 2" Click="BtnGoToPageTwo_Click" VerticalAlignment="Center" HorizontalAlignment="Center"></Button>
</Grid>
</Page>
Page1.xaml.cs
Page1.xaml.cs
private void BtnGoToPageTwo_Click(object sender, RoutedEventArgs e)
{
App.ParentWindowRef.ParentFrame.Navigate(new Page2());
}
Page2.xaml
Page2.xaml
<Page x:Class="MultiplePageOpeninSameScreen.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MultiplePageOpeninSameScreen"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Name="TxtBlockSomeTxt" Text="This is Page Two" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Grid.Row ="1" Name="BtnGoToPageOne" Content="Go To Page One" Click="BtnGoToPageOne_Click" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
</Grid>
</Page>
Page2.xaml.cs
Page2.xaml.cs
private void BtnGoToPageOne_Click(object sender, RoutedEventArgs e)
{
App.ParentWindowRef.ParentFrame.Navigate(new Page1());
}
这篇关于如何在同一窗口中打开新的XAML页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文