簡體   English   中英

如何在同一窗口中打開新的XAML頁

[英]How to open a new XAML page in the same window

我有兩個XAML頁面,當您單擊其中一個按鈕時,將打開另一個頁面。 我的代碼是:

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

CookingMenu是XAML頁面的名稱。 此代碼在新窗口中打開頁面,但是有沒有辦法在同一窗口中打開頁面? 這兩頁的尺寸相同。

希望這可以幫助:

App.xaml.cs

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

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

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

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

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

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

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

您應該看看Frame控件,這里是MSDN文檔

這是Paul Stovell的一個很好的例子 ,您應該研究一下。

可能最簡單的方法是使用Frame控件,就像其他人在這里建議的那樣,但是我想展示一種不同的方法,可以將窗口的內容更改為另一個,請嘗試以下代碼:

CookingMenu Testing = new CookingMenu();

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM