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

发布于 2024-12-06 05:19:35 字数 1065 浏览 0 评论 0原文

我是 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,然后我不想启动该计时器。

I m new in WPF,
i am developing a navigation application of 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>

and i have some page which display in this navigarionwindow like

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

How can i know which page is running currently under NavigationWindow.xaml.cs file?

I have a timer in this navigation window that want to check if current page is home.xaml then i don't want to start that timer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨离汐 2024-12-13 05:19:35

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

You can get current running page in code behind using CurrentSource property of navigation window. As per your requirements, it's done using NavigationService.Navigate() method like below :

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>

It's working fine for me. Hope this solve your problem. Please feel free to ask if it not solve.

UPDATE :

If you want to navigate page using class name instead of Uri then you can get current source like :

MessageBox.Show(((NavigationWindow)this).NavigationService.Content.GetType().Name.ToString() + ".xaml");
猫卆 2024-12-13 05:19:35

我有类似的问题。乌彭德拉上面接受的答案引导我走向正确的方向。我的问题是我在框架内使用不同的 WPF 页面。我需要确定框架内显示的是哪个页面。我是这样想出来的。

    Object CurrentPage;

    private void LSK_1L_Click(object sender, RoutedEventArgs e)
    {
        CurrentPage = MCDU_Page_Frame.Content.GetType();
    }

如果使用 CurrentPage.ToString(),则 CurrentPage 对象将成为加载页面的类名;

I had a similar problem. Upendra's accepted answer above lead me in the right direction. My problem was I was using different WPF Pages inside a FRAME. I needed to determine what page was being displayed inside the frame. Here's how I figured it out.

    Object CurrentPage;

    private void LSK_1L_Click(object sender, RoutedEventArgs e)
    {
        CurrentPage = MCDU_Page_Frame.Content.GetType();
    }

The CurrentPage object became the class name of the loaded page if used CurrentPage.ToString();

相思碎 2024-12-13 05:19:35

我通过查看容器窗口的 NavigationService 的 Content 属性找到了当前页面。

I found out my current page by looking at the Content property of the NavigationService of my container window.

雨巷深深 2024-12-13 05:19:35

如果我们想知道当前页面的完整路径并显示在框架内,那么我们可以使用:

string currentpage = Myframe.CurrentSource.OriginalString.ToString().Replace("yoursolutionname;component/", "");

if we want to known current page with full path which display inside frame then we can use that:

string currentpage = Myframe.CurrentSource.OriginalString.ToString().Replace("yoursolutionname;component/", "");
热情消退 2024-12-13 05:19:35

在每个页面中使用页面加载事件来删除返回条目并仅显示

页面加载内的当前页面使用NavigationService函数RemoveBackEntry()

Use Page Load Event in Every Page to remove Back Entries and to show only Current Page

Inside Page Load Use NavigationService function RemoveBackEntry()

余生再见 2024-12-13 05:19:35

NavigationWindow 有一个名为 CurrentSource 的属性,它是上次导航页面的 URI

The NavigationWindow has a property called CurrentSource which is the URI of the last page navigated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文