question

zehguilherme avatar image
0 Votes"
zehguilherme asked LeonLu-MSFT commented

How can I create a verification flow for my pages in the shell?

I have a total of 4 pages in the application

  • 2 in the shell - TabBar

  • The other 2 outside of it

When installing the app for the first time, the user must be taken to the Login screen, as he does not have yet been authenticated. If it is already authenticated (it has a token), it is taken to the Notifications screen.

The problem I am facing is that even though these checks are working (user being taken to the correct screens), when he goes to the Login page (either because it is his first installation of the app or because his account has already been closed and he wants to open another) a check that I put on the notifications page (in the OnAppearing() function) is triggered at the same time as the Login page is shown next.

I wonder if anyone would know a possible solution.

AppShell.xaml

xaml
<TabBar>
        <Tab Icon="notificacao_icone.png"
             Title="Notificações">
            <ShellContent ContentTemplate="{DataTemplate local:NotificacoesPage}" />
        </Tab>

        <Tab Icon="configuracoes_icone.png"
             Title="Configurações">
            <ShellContent ContentTemplate="{DataTemplate local:ConfiguracoesPage}" />
        </Tab>
</TabBar>


AppShell.xaml.cs

cs
public partial class AppShell : Shell
    {
        public AppShell()
        {
            InitializeComponent();

            Routing.RegisterRoute(nameof(LoginPage), typeof(LoginPage));
            Routing.RegisterRoute(nameof(TokenPage), typeof(TokenPage));
            Routing.RegisterRoute(nameof(NotificacoesPage), typeof(NotificacoesPage));
            Routing.RegisterRoute(nameof(NotificacaoDetalhePage), typeof(NotificacaoDetalhePage));
            Routing.RegisterRoute(nameof(ConfiguracoesPage), typeof(ConfiguracoesPage));

            MainThread.BeginInvokeOnMainThread(async () =>
            {
                await Task.Delay(500);

                // Carrega configurações do banco de dados
                var _config = BdRepository.tableExists("Configuracao");

                try
                {
                    if (!_config)
                    {
                        throw new Exception();
                    }
                    else
                    {
                        var _configuracao = ConfiguracaoRepository.GetAll();

                        if (string.IsNullOrEmpty(_configuracao.cad_token))
                        {
                            throw new Exception();
                        }
                        else
                        {
                            if (_configuracao.cad_token_validado_sn == "S")
                            {
                                // Redireciona para tela de notificacoes                        
                                await Shell.Current.GoToAsync($"{nameof(NotificacoesPage)}", false);
                            }
                            else
                            {
                                // Token nao validado, redireciona para tela de Token
                                await Shell.Current.GoToAsync($"{nameof(TokenPage)}", false);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // Redireciona para o cadastro
                    await Shell.Current.GoToAsync($"{nameof(LoginPage)}", false);
                }

            });
        }
    }
dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I put on the notifications page (in the OnAppearing() function) is triggered at the same time as the Login page is shown next.

Could you give me more information about it? notifications page and Login page will be triggered at the same time that is so wired, if the page will display, OnAppearing method will be executed. Do you show two pages at the same time?

0 Votes 0 ·

No, in fact they are not being shown at the same time, I meant that the notification page is being loaded first, even when it really wasn't supposed to be happening.

For example, when the user installs the app for the first time, where the login page should only be loaded and shown first. But at the moment, it is occurring that the notification page and its methods, are being executed before (still on the splash screen), when in fact only the login page should be executed at that first moment.

I don't know if it's clear yet.

0 Votes 0 ·

First, you should set the AppShell as the mainPage( register the page in the appshell background code). Then you can use Shell.Current.GoToAsync($"{nameof(LoginPage)}", false); to open the new page.

0 Votes 0 ·

0 Answers