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