Go to front page in C # WPF

I use navigation services in WPF. To go to the page, I use:

this.NavigationService.Navigate(new MyPage()); 

To return, I use:

 this.NavigationService.GoBack(); 

But how can I return to the first page (the "Home" button) without using

 this.NavigationService.Navigate(new FirstScreen()); 

Because I donโ€™t want to create a new first page, just go back to it? And I do not always open new screens in the same order.

+4
source share
4 answers

One way could be to use the RemoveBackEntry method until there is only one entry left in the back stack of the navigation service. If there is only one entry, just Navigation.GoBack()

+6
source

Here's a potential solution, however, there may be a โ€œbest practiceโ€ method that I don't know about at the moment:

 while(this.NavigationService.CanGoBack) { this.NavigationService.GoBack(); } 

CanGoBack returns true if there are entries in the back navigation history and, as such, GoBack () will execute until it returns false. Theoretically, this should return you to the origin, or, in other words, to the first page.

+4
source

Consider the implementation of PageFunction<T> . This class (derived from Page ) is designed for scripting when you want to relax on the start page. It can return / provide a type T object as a result after the "completion" of the use case (function).

More details (better than msdn) can be found here http://paulstovell.com/blog/wpf-navigation

+2
source

When I used your code, he said this:

TemplateScanControl does not contain a definition for NavigationService, and there is no NavigationService extension method available that takes the first argument of type TemplateScanControl (did you miss the using directive or the assembly reference?)

0
source

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


All Articles