简体   繁体   English

打开页面而不返回或关闭

[英]Open page without back or close

I'd like to redirect user to the main page after successful login, so I don't want him to have a back button or be able to get to the login page again. 成功登录后,我想将用户重定向到主页,所以我不希望他有后退按钮或能够再次进入登录页面。

I have redirected user to the login page with something like this: 我已经将用户重定向到登录页面,如下所示:

if (isAuthenticated)
{
    MainPage = new General.Pages.MainPage();
}
else
{
    MainPage = new Account.Pages.Login();
}

I'm using this code for successful login, which doesn't work and gives an exception: 我正在使用以下代码成功登录,但无法正常工作并给出了异常:

await Navigation.PopAsync(false);
await Navigation.PushAsync(new General.Pages.MainPage());

The given exception is: 给定的例外是:

System.InvalidOperationException: PopAsync is not supported globally on Android, please use a NavigationPage. System.InvalidOperationException:Android上不全局支持PopAsync,请使用NavigationPage。

And no need to mention that the mentioned NavigationPage doesn't work neither. 无需提及提到的NavigationPage也不起作用。

There are 2 ways you could approach this. 有两种方法可以解决此问题。

  1. Set you App.MainPage initially in the constructor of App.cs as your home page. 首先在App.MainPage的构造函数中将App.MainPage设置为主页。 Then in your home page's OnAppearing event or if it has a ViewModel then its Initialising method check if user is authenticated. 然后在您的主页的OnAppearing事件中,或者如果它具有ViewModel,则它的Initialization方法将检查用户是否已通过身份验证。 If yes load his data. 如果是,请加载他的数据。 If not the show your Login page as a Modal. 如果不是,则将您的登录页面显示为模态。 Once login is successful, you can pop the modal of login page and load the data for the home page. 登录成功后,您可以弹出登录页面的模式并加载主页数据。 Also if required you can handle the back button event in the login page to not allow the user to go out of the modal page. 同样,如果需要,您可以在登录页面中处理后退按钮事件,以不允许用户退出模式页面。 Drawback - The home page would be visible for a second before the login modal shows up. 缺点-在显示登录模式之前,主页将显示一秒钟。

  2. In the constructor of App.cs check if the user is authenticated. 在App.cs的构造函数中,检查用户是否已通过身份验证。 If user is authenticated then show home page by setting it as the MainPage . 如果用户通过了身份验证,则通过将其设置为MainPage显示主页。 Else set the MainPage as login page. 否则将MainPage设置为登录页面。 Once login is successful again set the MainPage . 登录成功后,再次设置MainPage
    Drawback - Checking isAuthenticated in the constructor might make the initial load of the application seem slower. 缺点-在构造函数中检查isAuthenticated可能会使应用程序的初始加载变慢。

And for the error you encounter is because you did not push any pages into the navigation stack. 对于您遇到的错误,是因为您没有将任何页面推入导航堆栈。 Hence your pop wont work. 因此,您的流行音乐将无法正常工作。

var firstPage = isAuthenticated ? new General.Pages.MainPage() : new Account.Pages.Login();
MainPage = new NavigationPage(firstPage);

If you will use 如果您使用

mentioned NavigationPage 提到的NavigationPage

properly, PopAsync will work. 正确地, PopAsync将正常工作。

I just figured it out, you can change MainPage at any point using this code: 我只是想通了,您可以使用以下代码随时更改MainPage

App.Current.MainPage = new General.Pages.MainPage();

I leave this post to be, if it can possibly help other users. 如果可以帮助其他用户,我将其保留。

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

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