如何在同一窗口中打开一个新的XAML页面

时间:2018-06-18 16:00:21

标签: c# wpf visual-studio xaml visual-studio-2017

我有两个XAML页面,当您单击其中一个按钮时,将打开另一个页面。我的代码是:

private void cookingButton(object sender, RoutedEventArgs e)
{
    CookingMenu cooking = new CookingMenu();
    cooking.Show();
}

CookingMenu是XAML页面的名称。此代码在新窗口中打开页面,但有没有办法在同一窗口中打开页面?两个页面都是相同的大小。

3 个答案:

答案 0 :(得分:2)

希望这会有所帮助:

//APP.XAML.CS RELATED

public partial class App : Application
{
    public static MainWindow ParentWindowRef;
}

// MAIN WINDOW RELATED

<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>


private void Window_Loaded(object sender, RoutedEventArgs e)
{
    App.ParentWindowRef = this;
    this.ParentFrame.Navigate(new Page1());
}

// PAGE 1 RELATED 

<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>

private void BtnGoToPageTwo_Click(object sender, RoutedEventArgs e)
{
    App.ParentWindowRef.ParentFrame.Navigate(new Page2());
}

// PAGE 2 RELATED 

<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>

private void BtnGoToPageOne_Click(object sender, RoutedEventArgs e)
{
    App.ParentWindowRef.ParentFrame.Navigate(new Page1());
}

答案 1 :(得分:0)

您应该查看Frame控件,MSDN doc

保罗·斯托维尔(Paul Stovell)你应该研究一下这个伟大的example

答案 2 :(得分:0)

可能最简单的方法是使用Frame控件,就像其他人在这里提出的那样,但我想以不同的方式显示,可以将窗口的内容更改为另一个,尝试此代码:

CookingMenu Testing = new CookingMenu();

private void cookingButton(object sender, RoutedEventArgs e)
{
this.Content = Testing.Content;
}