您可以从app.xaml.cs更改选项卡式页面的子导航堆栈吗?

时间:2019-10-07 21:47:05

标签: c# xamarin.forms

当用户重新打开我的应用程序时,我试图设置自动登录,以便它将检查保存在app.current.properties中的现有登录凭据,并在找到某些凭据时,将用户导航到正确的选项卡式页面,然后进行更改当前页面,从默认登录屏幕到帐户主页。

我尝试了一些通过研究看来应该可行的方法,特别是访问所涉及的子页面(登录)并通过其导航元素将新页面推送到该页面上。这没有用,我也没有尝试过实现其他一些麻烦的解决方案。

在App.xaml.cs中

protected override void OnStart()
        {
            AuthService.LoadUserCredentials();

            if (AuthService.authenticated)
            {
                var page = new MainTabs();
                page.CurrentPage = page.Children[2];

                ContactService.RefreshData();
                var page2 = new PinCodePage(false);
                page.CurrentPage.Navigation.PushAsync(page2).ConfigureAwait(false);

                MainPage = page;
            }
        }

应该注意,我的应用程序的设置方式是,整个应用程序的主页是一个选项卡式页面,共有3个页面,第三个页面是“登录”页面。当用户正常成功登录后,该页面上会添加一个新页面“帐户”,并弹出“登录”页面,从而有效地将其删除。

预期结果是,当用户在关闭应用程序/在登录后将其关闭后打开该应用程序时,它将检查用户存储的信息,并找到该信息,然后将“登录”页面替换为“帐户”页面。如果找不到任何内容,它将忽略替换登录页面,并将其留在那里。

1 个答案:

答案 0 :(得分:1)

您可以尝试一下(这里是一个简单的示例,您可以使用页面和数据替换):

protected override void OnStart()
    {
       if (App.Current.Properties.ContainsKey("isLogin"))
        {
            bool isLogin = (bool)App.Current.Properties["isLogin"];
            if (isLogin)
            {
                TabbedPage p = MainPage as TabbedPage;
                var navigationPage = new NavigationPage(new AccoutPage());
                navigationPage.IconImageSource = "tab_accout.png";
                navigationPage.Title = "Accout";
                p.Children.Add(navigationPage);
                p.Children.RemoveAt(2);
                p.CurrentPage = navigationPage;
            }
        }
    }

更新

public partial class LoginPage : ContentPage
{
    public LoginPage()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {

        App.Current.Properties["isLogin"] = true;
        App.Current.SavePropertiesAsync();
        TabbedPage p = App.Current.MainPage as TabbedPage;
        var navigationPage = new NavigationPage(new AccoutPage());
        navigationPage.IconImageSource = "tab_accout.png";
        navigationPage.Title = "Accout";
        p.Children.Add(navigationPage);
        p.Children.RemoveAt(2);
        p.CurrentPage = navigationPage;
    }
}