【问题标题】:Restore navigation stack when Xamarin.Forms app resumesXamarin.Forms 应用恢复时恢复导航堆栈
【发布时间】:2019-04-26 15:58:44
【问题描述】:

我正在构建一个包含这些页面的应用程序,按顺序:

(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呢?我怎样才能保存它们?

有没有办法阻止这种情况发生?

【问题讨论】:

    标签: xamarin xamarin.forms navigation state-management


    【解决方案1】:

    您可以在应用进入睡眠模式或 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.
                 }
        }
    

    【讨论】:

      【解决方案2】:

      好的,我能够通过在 MainPage 启动方法上添加断点来检测问题。

      基本上我要做的就是使用一个简单的布尔变量 (_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 构造函数中的调用之外,没有其他调用此页面,因此框架必须调用它,因为它是应用程序的主页。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        • 2014-07-10
        • 2021-04-24
        • 2019-01-14
        • 1970-01-01
        • 2013-05-20
        • 2011-07-27
        相关资源
        最近更新 更多