简体   繁体   English

转到C#WPF中的第一页

[英]Go to first page in C# WPF

I am using navigation services in WPF. 我在WPF中使用导航服务。 For navigating to page I use: 要导航到页面,我使用:

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

For going back I use: 对于回溯,我使用:

this.NavigationService.GoBack();

But how do I go back to the first page (Home button) without using 但是如何不使用返回到第一页(“主页”按钮)

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

Because I do not wish to create new first page, just go back to it? 因为我不想创建新的首页,所以回到它吗? And I don't always open new screens in the same order. 而且我并不总是以相同的顺序打开新屏幕。

One way could be to use method RemoveBackEntry until there is only a single entry left in the back stack of navigation service. 一种方法是使用方法RemoveBackEntry,直到导航服务的后堆栈中仅剩一个条目为止。 Once there is only a single entry simple do Navigation.GoBack() 一旦只有一个条目,就可以简单地进行Navigation.GoBack()

Here's a potential solution, however there may be a 'best practice' method that i'm not currently aware of: 这是一个潜在的解决方案,但是我目前尚不知道有一种“最佳实践”方法:

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

CanGoBack returns true if there are entries in the back navigation history and as such GoBack() will be executed until it returns false. 如果后退导航历史记录中有条目,则CanGoBack将返回true,因此,将执行GoBack(),直到返回false为止。 In theory this should get you back to the origin, or in other words the first page. 从理论上讲,这应该使您回到原点,换句话说就是第一页。

Consider implementing PageFunction<T> . 考虑实现PageFunction<T> This class (derived from Page ) is designed for scenarios when you want to unwind to a 'start' page. 此类(从Page派生)是为要展开到“开始”页面的方案设计的。 It can return/provide an object of type T as a result upon 'completion' of the use case (function). 当用例(函数)“完成”时,它可以返回/提供type T的对象。

More detail (better than msdn) can be found here http://paulstovell.com/blog/wpf-navigation 更多详细信息(比msdn更好)可以在这里找到: http://paulstovell.com/blog/wpf-navigation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM