繁体   English   中英

在WPF导航NavigationWindow中获取当前运行页面

[英]Get Current running page in 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" Width="1024" Source="Home.xaml"
    WindowState="Maximized" ResizeMode="NoResize" ShowsNavigationUI="False" WindowStyle="None" Cursor="Arrow" Closing="NavigationWindow_Closing"></NavigationWindow>

我有一些页面显示在这个navigarionwindow中

<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文件下运行?

我在这个导航窗口中有一个计时器,想要检查当前页面是否为home.xaml,然后我不想启动该计时器。

您可以使用导航窗口的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>

NavWindow.xaml.cs:

namespace WPFTest
{
    public partial class MyNavWindow : NavigationWindow
    {
        public MyNavWindow()
        {
            InitializeComponent();
        }

        private void NavigationWindow_Navigated(object sender, NavigationEventArgs e)
        {
            MessageBox.Show(((NavigationWindow)this).CurrentSource.ToString());
        }
    }
}

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>

ShopList.xaml.cs:

namespace WPFTest
{
    public partial class ShopList : Page
    {
        public ShopList()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new System.Uri("ProductList.xaml", UriKind.Relative));
        }
    }
}

ProductList.xaml:

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

它对我来说很好。 希望这能解决你的问题。 如果不解决,请随时询问。

更新:

如果您想使用类名而不是Uri来导航页面,那么您可以获得当前的源代码:

MessageBox.Show(((NavigationWindow)this).NavigationService.Content.GetType().Name.ToString() + ".xaml");

我遇到了类似的问题。 Upendra上面接受的答案引导我朝着正确的方向前进。 我的问题是我在FRAME中使用不同的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()

NavigationWindow有一个名为CurrentSource的属性,它是导航的最后一页的URI

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM