代码之家  ›  专栏  ›  技术社区  ›  George M Ceaser Jr

Xamarin表单恢复特殊处理

  •  1
  • George M Ceaser Jr  · 技术社区  · 7 年前

    当我的Xamarin Forms跨平台应用程序恢复时,我想截取导航并传输到加载页面,我会检查某些数据是否已更改等。执行此处理后,如果没有任何更改,我想恢复到onresume将带我访问的原始页面的导航。

    因此,首先,我在我的应用程序中保存了一个页面实例,用于检查变量中数据的更改。xaml。cs文件。当我的应用程序启动时,我创建了一个我想要的页面实例并导航到它。下面是我的应用程序中的代码。xaml。cs文件:

                gobj_BaseLoadingPage = new baseloadingpage();
    
                if (Application.Current.MainPage == null)
                {
                    Application.Current.MainPage = new NavigationPage(gobj_BaseLoadingPage);
                }
    

    现在在简历中,我想回到这个加载页面。我尝试了下面代码的许多变体,但似乎无法使其正常工作。使用下面的代码,按基本加载页面永远不会显示,应用程序只会返回到暂停时所在的页面。

            App.Current.MainPage = (Page)gobj_BaseLoadingPage.Parent;
            gobj_BaseLoadingPage.Parent = null;
            Application.Current.MainPage.Navigation.PushAsync(gobj_BaseLoadingPage);
    

    如果只对页面执行pushasync而不清除其父级,则会出现一个错误,即页面不能已经有父级。

    如有任何建议,将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Benl    7 年前

    使用 PopToRootAsync , InsertPageBefore PopAsync . (未显示任何状态保存):

    Page rootPage;
    
    public App()
    {
      InitializeComponent();
    
      rootPage = new OriginalRootPage();
      MainPage = new NavigationPage(rootPage);
    }
    
    protected override async void OnResume()
    {
      // Handle when your app resumes
      Debug.WriteLine("Resume");
    
      var nav = MainPage.Navigation;
    
      // Pops all but the root Page off the navigation stack, 
      // making the root page of the application the active page.
      await nav.PopToRootAsync();
    
      if (!(nav.NavigationStack.First() is NewRootPage))
      {
        // Insert a page on the navigation stack before the original rootPage
        var newRootPage = new NewRootPage();
        nav.InsertPageBefore(newRootPage, rootPage);
    
        // Pops the original rootPage, making the newRootPage the only page on the stack
        await nav.PopAsync();
      }
    }