Get Current running page in WPF navigation NavigationWindow
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="Maximi开发者_开发知识库zed" 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.
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");
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();
I found out my current page by looking at the Content property of the NavigationService of my container window.
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/", "");
Use Page Load Event in Every Page to remove Back Entries and to show only Current Page
Inside Page Load Use NavigationService function RemoveBackEntry()
The NavigationWindow
has a property called CurrentSource
which is the URI of the last page navigated
精彩评论