Using variables from different .cs files

User_101User_101 Member ✭✭
edited September 12 in Xamarin.Forms

This could be silly but I am lacking the basics I guess.

I have a login page where I am sending username,pwd entries and using POST api.

loginpage.cs:
var token is declared here which is the response received from POST.

public partial class LoginPage : ContentPage
{
    public async void Btn_clicked(object sender,Eventargs e)
    {
    var token;
    //more code
    }
}

Now I want to use this variable in another file, say retrievepage.cs

public partial class RetrievePage
{
    public RetrievePage()
    {
        InitializeComponent();
    }
    public async void New_btn(object sender,Eventargs e)
    {
            ////use the token variable here.
    }
}

How can this be done?
I am going to use GET api here. Is this even allowed to do so?

Tagged:

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    pass them inside RetrievePage constructor

  • User_101User_101 Member ✭✭

    @AlessandroCaliaro said:
    pass them inside RetrievePage constructor

    sorry I could not do it. can u explain this further

  • ColeXColeX Member, Xamarin Team Xamurai

    Use Xamarin.Essentials: Preferences .

    Store token

    public partial class LoginPage : ContentPage
    {
        public async void Btn_clicked(object sender,Eventargs e)
        {
        var token;
        Preferences.Set("my_key", token);
        }
    }
    

    Get token

    public partial class RetrievePage
    {
        public RetrievePage()
        {
            InitializeComponent();
        }
        public async void New_btn(object sender,Eventargs e)
        {
               var token = Preferences.Get("my_key", "default_value");
        }
    }
    
  • User_101User_101 Member ✭✭

    There is an issue here where a method is not called at all:

    In the above RetrievePage.cs file:

    public RetrieveVisits()
        {
            InitializeComponent();
        GetRecords();
    
        }
    
        public async void GetRecords()
        {
            await DisplayAlert("test", "1", "@");    ////THIS METHOD IS NOT CALLED AT ALL. THIS DOES NOT GET DISPLAYED
         }
    
        public async void logout_clicked(object sendedr,EventArgs e)
        {
            await DisplayAlert("testing","ok","ok");   ////THIS IS FINE
       }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    async… await… you should know how it works before write the code

  • ColeXColeX Member, Xamarin Team Xamurai

    Never call a asynchronous method in class constructor .

  • User_101User_101 Member ✭✭

    Removed them, but still no changes

    public RetrieveVisits()
      {
         InitializeComponent();
         GetRecords();
    
         }
    
    public void GetRecords()
     {
             DisplayAlert("test", "1", "@");    ////THIS METHOD IS NOT CALLED AT ALL. THIS DOES NOT GET DISPLAYED
    
        ///I am writing this  because other code in this method does not get executed. So trying with DisplayAlert to see what is happening here
      }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    you have not to add or remove things, you have to understand how async and await work. DisplayAlert should be awaited. If you need to display something you can use System.Diagnostic.Debug.WriteLine

  • User_101User_101 Member ✭✭
    edited September 12

    In my code:

    LoginPage.cs:

    public partial class LoginPage : ContentPage
    {
        public async void Btn_clicked(object sender,Eventargs e)
        {
        var token;
        await Navigation.PushAsync(new RetrieveVisits()); ////IS THERE ANY ERROR HERE? I am unable to figure it out
    //more code
        }
    }
    

    RetrieveVisitis.cs:

    public RetrieveVisits()
    {
        InitializeComponent();
        GetRecords();
    
        }
    
    public void GetRecords()
    {
        Debug.WriteLine("testing"); ////not executed
    
    }
    
  • ColeXColeX Member, Xamarin Team Xamurai

    You have to ensure LoginPage is wrapped inside Navigation Stack first before use PushAsync .

    Otherwise , you could use PushModalAsync instead to push into modal navigation .

  • User_101User_101 Member ✭✭
    edited September 13

    Thanks @ColeX
    I tried out with PushModalAsync, no improvement yet.

    LoginPage.cs:

    public partial class LoginPage : ContentPage
        {
    
            public LoginPage()
            {
                InitializeComponent();
            }
            public async void Btn_Clicked(object sender, EventArgs e)
            {
            //code
            await Navigation.PushModalAsync(new RetrieveVisits());
        }
    }
    

    RetrieveVisits.cs:

     public partial class RetrieveVisits : ContentPage
        {
            public RetrieveVisits()
            {
                    InitializeComponent();
                     NewRecords();
             }
    
            private void NewRecords()
             {
                    Debug.WriteLine("testing"); ///THIS DOES NOT EXECUTED
            //more code
    
             }
    
            private void logout_clicked(object sender,EventArgs e)
             {
                  //some code. Gets executed
             }
    }
    
  • User_101User_101 Member ✭✭

    Oh finally I got my mistake.

    I put my whole code in comments, and just trying with Debug.writeline.
    Removed comments and started to work on it, and then got it. Thanks all :)

Sign In or Register to comment.