.net 在WPF导航窗口中获取当前正在运行的页面
我是WPF的新手, 我正在开发WPF的导航应用程序.net 在WPF导航窗口中获取当前正在运行的页面,.net,wpf,navigation,navigationwindow,.net,Wpf,Navigation,Navigationwindow,我是WPF的新手, 我正在开发WPF的导航应用程序 <NavigationWindow x:Class="KioskTraffic.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="768" Widt
<NavigationWindow x:Class="KioskTraffic.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="768" Width="1024" Source="Home.xaml"
WindowState="Maximized" ResizeMode="NoResize" ShowsNavigationUI="False" WindowStyle="None" Cursor="Arrow" Closing="NavigationWindow_Closing"></NavigationWindow>
我有一些页面显示在导航窗口中,如
<Page x:Class="KioskTraffic.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"
mc:Ignorable="d"
Height="768" Width="1024"
Title="Page1">
如何知道NavigationWindow.xaml.cs文件下当前正在运行的页面
我在这个导航窗口中有一个计时器,它想检查当前页面是否为主页。xaml,那么我不想启动该计时器。导航窗口的
有一个名为CurrentSource
的属性,它是导航的最后一个页面的URI您可以使用的CurrentSource属性在代码隐藏中获取当前正在运行的页面导航窗口。根据您的要求,可以使用如下所示的NavigationService.Navigate()方法完成此操作:
NavWindow.xaml:
<NavigationWindow x:Class="WPFTest.MyNavWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="768" Width="1024" Source="ShopList.xaml" Grid.Row="1"
WindowState="Maximized" ResizeMode="NoResize" ShowsNavigationUI="True" WindowStyle="SingleBorderWindow" Cursor="Arrow" Navigated="NavigationWindow_Navigated">
</NavigationWindow>
ShopList.xaml:
<Page x:Class="WPFTest.ShopList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShopList">
<Grid>
<Label Height="28" Margin="81,12,99,0" Name="label1" VerticalAlignment="Top" FontSize="16" FontWeight="Bold">Shop List</Label>
<Button Name="btnNext" Content="Go to Product list" Width="150" Height="30" Margin="0,50,0,0" Click="btnNext_Click"></Button>
</Grid>
<Page x:Class="WPFTest.ProductList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ProductList">
<Grid>
<Label Height="28" Margin="81,12,99,0" Name="label1" VerticalAlignment="Top" FontSize="16" FontWeight="Bold">Product List</Label>
</Grid>
</Page>
ProductList.xaml:
<Page x:Class="WPFTest.ShopList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShopList">
<Grid>
<Label Height="28" Margin="81,12,99,0" Name="label1" VerticalAlignment="Top" FontSize="16" FontWeight="Bold">Shop List</Label>
<Button Name="btnNext" Content="Go to Product list" Width="150" Height="30" Margin="0,50,0,0" Click="btnNext_Click"></Button>
</Grid>
<Page x:Class="WPFTest.ProductList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ProductList">
<Grid>
<Label Height="28" Margin="81,12,99,0" Name="label1" VerticalAlignment="Top" FontSize="16" FontWeight="Bold">Product List</Label>
</Grid>
</Page>
我也有类似的问题。上面奥本德拉接受的答案引导我走向正确的方向。我的问题是我在一个框架内使用了不同的WPF页面。我需要确定框架内显示的页面。我是这样想的
Object CurrentPage;
private void LSK_1L_Click(object sender, RoutedEventArgs e)
{
CurrentPage = MCDU_Page_Frame.Content.GetType();
}
如果使用CurrentPage.ToString(),则CurrentPage对象成为加载页面的类名 我通过查看“我的容器”窗口的NavigationService的Content属性找到了我的当前页面。如果我们想知道显示在框架内的完整路径的当前页面,那么我们可以使用:
string currentpage = Myframe.CurrentSource.OriginalString.ToString().Replace("yoursolutionname;component/", "");
在每个页面中使用“页面加载事件”可删除返回条目并仅显示当前页面
在页面加载内部使用NavigationService函数RemoveBackEntry(),但在WPF中,它只在第一次给我URI,而不是在那之后,我正在这样做。NavigationService.navigate(new MyNewpage());用于导航到另一个页面。问题需要更多上下文和所有者的积极参与。我已经以简单的方式编辑了我的问题,仍在等待答案…我在第一次加载我的第一个页面时遇到错误“对象引用未设置实例的对象”,这是确定的,但当我单击按钮“this.NavigationService.navigate”(new TimeoutDisplay());“它给了我那个错误。我想从任何一个页面获取页面名。@ViralSarvaiya:代码中应该有System.Uri(“yourpage.xaml”,UriKind.Relative)而不是TimeoutDisplay()。这里我用一个类来代替Uri。是的,那个错误来自第行:MessageBox.Show(((导航窗口)this.CurrentSource.ToString());如果您使用类名而不是Uri,那么您可以像我更新的答案一样使用它。@ViralSarvaiya:您解决了问题还是继续?您能解释一下这是如何回答问题的吗?
string currentpage = Myframe.CurrentSource.OriginalString.ToString().Replace("yoursolutionname;component/", "");