Cant make global variable show up after Navigation.PushModalAsync

The global variable doesnt show up after login is successful, however it shows up when i refresh the page.

(Class)

  public static class Login_information
    {
        public static int Global_login_id;
        public static string User_Firstname ;
        public static string User_Lastname; 
    }

(Loginpage)

private void Button_Pressed(object sender, EventArgs e)
{
     if (Password_input.Text == item.Password)
            {
                DisplayAlert("Login success", "Successfull login", "OK");
                Navigation.PushModalAsync(new NavigationBarPage());
                Login_information.Global_login_id = item.Id;
                Login_information.User_Firstname = item.FirstName;
                Login_information.User_Lastname = item.LastName;

            }
}

(Profilepage)

  public ProfilePage()
        {


            InitializeComponent();
            Update();
            Login_name.Text = Login_information.User_Firstname + " " + Login_information.User_Lastname;


        }

Best Answer

  • edited June 2018 Accepted Answer
         if (Password_input.Text == item.Password)
                    {
                        Login_information.Global_login_id = item.Id;
                        Login_information.User_Firstname = item.FirstName;
                        Login_information.User_Lastname = item.LastName;
                        await DisplayAlert("Login success", "Successfull login", "OK");
                        await Navigation.PushModalAsync(new NavigationBarPage());
    
    
                    }
    

    Declaring the variable before pushmodelasync made it work for me!

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @arminmosaffaie

    Not sure this will fix the issue without seeing more code (but it might), but you should always await calls to async methods. In your code snippet for Login page:

    private void Button_Pressed(object sender, EventArgs e)
    {
     if (Password_input.Text == item.Password)
            {
                DisplayAlert("Login success", "Successfull login", "OK");
                Navigation.PushModalAsync(new NavigationBarPage());
                Login_information.Global_login_id = item.Id;
                Login_information.User_Firstname = item.FirstName;
                Login_information.User_Lastname = item.LastName;
    
            }
    }
    

    since you are not awaiting the call to Navigation.PushModalAsync(new NavigationBarPage()); the following lines of code will run before that method completes, possibly causing the new page to be created before the Login_information.Global_login_id, Login_information.User_Firstname, and Login_information.User_Firstname properties are set. So try this (which even if it does not fix the specific issue you should do anyway to follow async best practices):

    private async void Button_Pressed(object sender, EventArgs e) // <-- Added the async keyword
    {
     if (Password_input.Text == item.Password)
            {
                DisplayAlert("Login success", "Successfull login", "OK");
                await Navigation.PushModalAsync(new NavigationBarPage()); // <-- Added the await keyword which will cause the following lines of code to not run until this method finishes. 
                Login_information.Global_login_id = item.Id;
                Login_information.User_Firstname = item.FirstName;
                Login_information.User_Lastname = item.LastName;
    
            }
    }
    

    I would need to see more code to see if setting those properties will actually cause the controls/views that are displaying the data on the target page to update.

  • arminmosaffaiearminmosaffaie Member ✭✭
    edited June 2018 Accepted Answer
         if (Password_input.Text == item.Password)
                    {
                        Login_information.Global_login_id = item.Id;
                        Login_information.User_Firstname = item.FirstName;
                        Login_information.User_Lastname = item.LastName;
                        await DisplayAlert("Login success", "Successfull login", "OK");
                        await Navigation.PushModalAsync(new NavigationBarPage());
    
    
                    }
    

    Declaring the variable before pushmodelasync made it work for me!

Sign In or Register to comment.