connersz
connersz

Reputation: 1223

How to close the current window in Xamarin forms?

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());
    }

}

Upvotes: 0

Views: 2966

Answers (1)

Mr.Ko&#231;ak
Mr.Ko&#231;ak

Reputation: 333

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

Upvotes: -1

Related Questions