Get current current page in WPF navigation NavigationWindow

I am new to WPF, I am developing a WPF navigation application,

<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 a page that appears in this navigation window, for example

 <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 do I know which page is currently running in the NavigationWindow.xaml.cs file?

I have a timer in this navigation window that wants to check if the current page is home.xaml, then I do not want to start this timer.

+6
source share
5 answers

You can get the current current page in code using the CurrentSource property of the navigation window. According to your requirements, this is done using the NavigationService.Navigate () method, as shown 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 works great for me. Hope this solves your problem. Please feel free to ask if he decides.

UPDATE:

If you want to navigate the page using the class name instead of Uri, you can get the current source, for example:

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

I had a similar problem. The Upendra answer accepted above led me in the right direction. My problem was that I used different WPF pages inside FRAME. I needed to determine which page was displayed inside the frame. This is how I understood it.

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

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

+3
source

I recognized my current page by looking at the Content property in the NavigationService of my container window.

+1
source

if we want to know the current page with the full path that displays inside the frame, we can use this:

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

NavigationWindow has a CurrentSource property, which is the URI of the last page being moved

-1
source

Source: https://habr.com/ru/post/897815/


All Articles