【发布时间】:2017-12-22 17:45:28
【问题描述】:
我对 WPF 很陌生,但现在真的很享受它。我有一个关于我正在尝试实现的页面导航的问题。
我有 MainWindow.xaml,里面有一个框架。 启动时,框架加载第 1 页。 第 1 页有一个按钮。 如果单击该按钮,则第二页将出现在框架内。 但是,我不想要这个。当用户单击第 1 页中的按钮时,我希望整个页面(MainWindow.xaml)导航到第 2 页。所以我需要更改实际的父页面本身。
我在按钮的 Click 事件中使用以下代码。我只是不知道如何引用父页面。
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new PageTwoReplacesMainPage());
}
如果我需要提供更多信息,请告诉我。 我希望这是有道理的,任何帮助将不胜感激。谢谢。
MainWindow.xaml 代码是...
<Window x:Class="TestWpfParentNavigation.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:TestWpfParentNavigation"
mc:Ignorable="d"
Title="MainWindow" MinHeight="500" MinWidth="700" Background="Beige">
<Grid>
<StackPanel>
<Frame x:Name="myMainFrame" NavigationUIVisibility="Hidden" Height="400" Width="600" Margin="0,50,0,0" />
</StackPanel>
</Grid>
</Window>
Page1.xaml 是...
<Page x:Class="TestWpfParentNavigation.PageOneWithButton"
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:TestWpfParentNavigation"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300"
Title="PageOneWithButton" Background="Bisque">
<Grid>
<Button Content="Go to the second page" Height="30" Width="150" Click="Button_Click" Margin="10,10,140,160" />
</Grid>
</Page>
Page2.xaml 是...
<Page x:Class="TestWpfParentNavigation.PageTwoReplacesMainPage"
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:TestWpfParentNavigation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="PageTwoReplacesMainPage" Background="Aqua">
<Grid>
</Grid>
</Page>
【问题讨论】:
-
请为您的 UI 提供 XAML。
-
MainWindow 不是页面,它是一个窗口。当您说您希望“整个页面”进行导航时,您是什么意思?
-
您是否尝试在网格中而不是堆栈面板中制作框架?那么它应该适合整个窗口。
-
我不希望第二页出现在框架中。我希望整个 MainWindow 显示第二页。
-
我正在尝试实现母版页类型的布局。我的页面将具有公共控件,因此我想将屏幕的一部分分配给公共控件,将其余部分分配给不同的页面内容。
标签: c# wpf navigation frame parent