我正在构建一个包含这些页面的应用程序,按顺序:
(P)
:ContentPage
(V)
:ContentView
在ContentPage
举办。
->
:正在执行的呼叫或代码。
(P) Main
-> If login not detected, automatically goes to:
(P) Startup
(P) Sign up, has these ContentViews as registration steps:
(V) Email and password
(V) Name
(V) Profile photo
-> Call to CrossMedia.Current.TakePhotoAsync
(V) Other details
-> Returns to Main, if has successful profile creation.
(P) Sign in
-> Returns to Main, if has successful login.
-> If login detected:
-> Load the content of the Main page.
因此,每当我锁定应用程序的屏幕或切换应用程序(例如打开相机,通过调用CrossMedia插件)时,应用程序将再次直接进入主页面,这会将用户引导回启动页面,以防万一没有检测到登录。
有什么方法可以解决这个问题吗?怎么样?我应该在OnSleep方法的某处保存导航堆栈吗?每个页面的DataContext怎么样?我怎么能保存它们?
有没有办法阻止这种情况发生?
当您的应用程序进入睡眠模式或您的contentPage消失时,您可以设置应用程序级别属性。如果确实如此,您可以跳过登录检查恢复方法,让用户恢复它离开的应用程序。
在App.xaml.cs中
public static bool IsSignUpInProgress{get;set;}
protected override void OnSleep()
{
if(Application.Current.MainPage==typeof(SignUpPage))
IsSignUpInProgress=true;
}
protected override void OnResume()
{
if(IsSignUpInProgress==false)
{
//Do your login check.
}
}
好的,我能够通过在MainPage启动方法上添加断点来检测问题。
基本上我要做的是通过使用一个简单的bool变量(_loaded
)来检查页面是否已经加载。
protected override async void OnAppearing()
{
base.OnAppearing();
IsBusy = true;
if (!_loaded && !await ViewModel.Auth.IsLoggedIn())
{
_loaded = true;
await App.NavigationHelper.NavigateModalAsync(new Startup(), false);
}
IsBusy = false;
}
当应用程序恢复时,即使切换应用程序,如相机,MainPage
正在执行OnAppearing
方法。
除了App.xaml.cs
构造函数之外,没有其他对此页面的调用,因此框架必须调用它,因为它是应用程序的主页面。