我的应用程序结构如下:
Main Page : Choose Login Type A or B
if the user choses login type A then navigate to > Page A1 Credentials for A type
if the user choses login type B then navigate to > Page B1 Credentials for B type
if Login is successfull for page A1 navigate to > Page A2
if Login is successfull for page B1 navigate to > Page B2
Page A1 --- Login Successful ---> Page A2
/
/
/
Page 1 \
\
\
Page B1 --- Login Successful ---> Page B2
如果用户在页面A1或B1,我希望允许他返回页面1,如果他愿意。但是,如果用户使用类型1或2登录,并到达A2或B2,他不应该被允许返回。
下面是我的代码:
在进入A2或B2页之前,我运行。(此代码在A1、B1页执行):
public static void RemoveAllPagesFromNavigation(INavigation Navigation)
{
var existingPages = Navigation.NavigationStack.ToList();
foreach (var page in existingPages)
{
if (page != null)
{
Navigation.RemovePage(page);
}
}
}
RemoveAllPagesFromNavigation(this.Navigation);
await Navigation.PushAsync(new PageA2()); // Or PageB2
但是函数RemoveAllPagesFromNavigation引发异常,指出NavigationStack的第一个元素为空;
我添加了一个条件,以跳过第一页为空的情况,函数运行,但最终用户仍然能够返回到第1页。
有人知道如何防止用户返回到页面1、A1或B2吗?
- 谢谢-谢谢
3条答案
按热度按时间q3aa05251#
Because you don't want user to be able to go back, use
Navigation.GoToAsync
instead of
Navigation.PushAsync
EXPLANATION:
"AppShell navigation" only uses Navigation stack when it has to.
I suspect you'll find that
foreach (var page in existingPages)
does not list ANY non-null pages; there is nothing on the stack.AppShell is remembering one "current" page elsewhere. (Internally? I'm not sure.)
GoToAsync
will make your new page the current page.FYI ALTERNATIVE:
There is an alternative navigation paradigm, NavigationPage that lacks appshell features such as "Routes". Advantage is simple, direct, programmatic control over the navigation stack. (Without the subtleties that AppShell introduces. One of which you have encountered.)
Consider using
NavigationPage
during the login sequence, only going to AppShell once user is in your main content. (Or omit AppShell completely, just use NavigationPage for your app.)To do so, in App.xaml.cs, replace:
MainPage = new AppShell();
with:
MainPage = new NavigationPage();
.Then use
NavigationPage
features to show first login page, etc. Don't use anything from that AppShell navigation doc I linked earlier.If after login success, you want to use AppShell navigation, do:
Application.Current.MainPage = new AppShell();
.THIRD ALTERNATIVE:
Do
Application.Current.MainPage = new MyNextFascinatingPage();
to step through the login sequence. Thus there is nowhere for user to go back to, during these steps. (If you want them to go back, you do so explicitly, by creating anew
copy of earlier page.)After login success, do:
Application.Current.MainPage = new AppShell();
.q0qdq0h22#
当您成功导航到页面A2或页面B2时,NavigationStack中将有3个页面,因此请删除前2个页面(页面1和(页面A1或页面B1)),然后页面A2或页面B2将成为根页面。
ifmq2ha23#
您可以使用
在您希望用户无法后退的页面上。