首页 文章

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

提问于
浏览
0

我有两个XAML页面,当你单击一个按钮时,另一个页面打开 . 我的代码是:

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

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

3 回答

  • 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());
    }
    
  • 0

    你应该看一下 Frame 控件这里是MSDN doc

    这是一个伟大的保罗斯托维尔example你应该看看 .

  • 0

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

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

相关问题