0

I have a Xamarin Forms PCL project that consists of several tabbed pages. When the app is started it checks to see if the user is logged in. If they aren't then it loads the login screen, otherwise it loads the MainPage (which creates the tabbed items).

The problem I'm having is that on the login screen I just want to close this window when they have logged in successfully but by using PopModalAsync, the page is unable to get the instance of itself (resulting in a null exception). The page then just sits there doing nothing until you restart it and it lets you in.

I need a way of closing the login screen but can't seem to find a definitive suggestion as to how I can do this.

App.cs:

private async void Login_Clicked(object sender, EventArgs e)
{
    if ((!string.IsNullOrWhiteSpace(username.Text) && !string.IsNullOrWhiteSpace(password.Text)))
    {
        indicator.IsVisible = true;
        indicator.IsRunning = true;
        LoggedInUserInfoRoot liuir = await WebDataAccess.LoginUser(username.Text, password.Text);

        try
        {
            if (liuir.user != null)
            {
                if (liuir.user.userId > 0)
                {
                    UserInfoRepository.UpdateUserInformation(liuir.user.userId, DateTime.Now, liuir.user.userName);
                    await WebDataAccess.SaveSwipesToCloud();
                    await Navigation.PushModalAsync(new MainPage());
                    //var poppedPage = await Navigation.PopModalAsync();
                }
                else
                {
                    DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                    indicator.IsVisible = false;
                    indicator.IsRunning = false;
                }
            }
            else
            {
                DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                indicator.IsVisible = false;
                indicator.IsRunning = false;
            }

        }
        catch (Exception ex)
        {
            ErrorRepository.InsertError(ex.ToString());
        }
    }
    else
    {
        DisplayAlert("Login", "You must enter both a username and password", "Ok");
        indicator.IsVisible = false;
        indicator.IsRunning = false;
    }
}

Login screen:

private async void Login_Clicked(object sender, EventArgs e)
{
    if ((!string.IsNullOrWhiteSpace(username.Text) && !string.IsNullOrWhiteSpace(password.Text)))
    {
        indicator.IsVisible = true;
        indicator.IsRunning = true;
        LoggedInUserInfoRoot liuir = await WebDataAccess.LoginUser(username.Text, password.Text);

        try
        {
            if (liuir.user != null)
            {
                if (liuir.user.userId > 0)
                {
                    UserInfoRepository.UpdateUserInformation(liuir.user.userId, DateTime.Now, liuir.user.userName);
                    await WebDataAccess.SaveSwipesToCloud();
                    var poppedPage = await Navigation.PopModalAsync();
                }
                else
                {
                    DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                    indicator.IsVisible = false;
                    indicator.IsRunning = false;
                }
            }
            else
            {
                DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                indicator.IsVisible = false;
                indicator.IsRunning = false;
            }

        }
        catch (Exception ex)
        {
            ErrorRepository.InsertError(ex.ToString());
        }
    }
    else
    {
        DisplayAlert("Login", "You must enter both a username and password", "Ok");
        indicator.IsVisible = false;
        indicator.IsRunning = false;
    }
}

MainPage:

public MainPage()
{
    Children.Add(new Clocking());
    Children.Add(new ChangePassword{ BackgroundColor = Color.FromHex("59a092") });
    Children.Add(new CompanySetup { BackgroundColor = Color.FromHex("59a092") });
    Children.Add(new Log { BackgroundColor = Color.FromHex("59a092") });
    isuserloggedin();
}

public async void isuserloggedin()
{
    bool isuserloggedin = false;
    if (UserInfoRepository.UserLoggedInTime() != null)
    {
        if (UserInfoRepository.UserLoggedInTime() < DateTime.Now.AddMinutes(-60))
        {
            isuserloggedin = false;
        }
        else
        {
            isuserloggedin = true;
        }
    }
    if (isuserloggedin == false)
    {
        //  MainPage = new Login { BackgroundColor = Color.FromHex("59a092") };
        await Navigation.PushModalAsync(new Login());
    }

}
Alan Clark
  • 974
  • 7
  • 10
connersz
  • 1,153
  • 3
  • 23
  • 64

1 Answers1

-1

My tabbed page

public class MyTabbedPage:TabbedPage

Dont forget to push your page with this

await Navigation.PushModalAsync(new NavigationPage(new MyTabbedPage()));

What i have more then your code is this

BackgroundColor = Color.FromRgb (76, 108, 139)
Device.OnPlatform(() => 
{
    this.Padding = new Thickness(0, 30, 0, 0);
});

and i add my pages with this function
void AddPage(string title, params View[] views) 
    {
        var content = new StackLayout();
        foreach (var view in views)
            content.Children.Add(view);
        this.Children.Add(new ContentPage 
        {
            Content = content,
            Title = title
        });
    }

Hope it helps

Mr.Koçak
  • 313
  • 4
  • 9
  • With this I get an error: System.MissingMethodException: Method 'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener' not found. – connersz Dec 05 '16 at 13:25
  • How come.. i have a tabbed page and i do it with this code.. and it works.. how do you create your tabbed page? – Mr.Koçak Dec 05 '16 at 13:39
  • I've added the MainPage code that shows how the tabbed pages are created. – connersz Dec 05 '16 at 14:07
  • I think that your problem doesnt come from creating tabbed page.. Check this post.. [missingmethod](http://stackoverflow.com/questions/8058832/system-missingmethodexception-method-not-found) And we are sacrifying time to help people so.. – Mr.Koçak Dec 06 '16 at 07:48
  • The problem is that the pages contain content. If I change them to inherit TabbedPage it throws up errors everywhere for a lot of the objects like 'DisplayAlert' and 'Navigation' (The name does not exist in it's current context'. – connersz Dec 06 '16 at 09:05
  • Its your main page that inherits tabbedpage not your content pages who will be tabs.. we are ok with that? – Mr.Koçak Dec 06 '16 at 10:14
  • The MainPage had always inherited from TabbedPage. I'm afraid the code you supplied does not work. – connersz Dec 06 '16 at 10:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129897/discussion-between-mr-kocak-and-connersz). – Mr.Koçak Dec 06 '16 at 11:17