0
votes

I debugged my own code and realize my btnofflinecount resets every time I navigate back and forth from main page to second page. Is it possible to not reset the counter after reload?

First, user will click btnOffline, which will redirect user to second page. Once user click on btnDone, it will go back to main page. At the same, I will take the start and end date time stamp based on button click. Right now, my end date time stamp will keep updating even though I have if statement to check the condition. (I will get the end date time stamp on btnDone event) I found out my counter resets when navigate back from Second Page to Main Page without triggering btnDone_Clicked yet. Does anyone know how to solve this?

Main Page

public partial class MainPage : ContentPage
{
    public string mainpagevalue;
    int offlinecount = 0;

    public MainPage()
    {
        InitializeComponent();

    }

    private void btnOffline_Clicked(object sender, EventArgs e)
    {
        offlinecount++;

        Navigation.PushAsync(new SecondPage(this, lblEndDT));


        if (offlinecount == 1)
        {
            string currentDT = DateTime.Now.ToString();
            lblStartDT.Text = currentDT;

        }


    }

Second Page

    public partial class SecondPage: ContentPage
    {
    Label lblEndDT;
    MainPage mainpage;
    int btnofflinedone = 0;

    public SecondPage()
    {
        InitializeComponent();
    }

    public SecondPage(MainPage mPage, Label endDT)
    {
        InitializeComponent();
    lblEndDT = endDT;
    mainpage = mPage;

    }
    protected void btnDone_Clicked(object sender, EventArgs e)
    {


        btnofflinedone++;

        if (btnofflinedone == 1)
        {
            string edt = DateTime.Now.ToString();
            lblEndDT.Text = edt;
            mainpage.mainpagevalue = lblEndDT.Text;

        }

        Navigation.PopAsync();


       }

     }
   } 
1
First, by "my btndonecount resets every time I navigate back and forth" do you mean btnofflinedone. because I don't find any btndonecount. Secondly, Why not set a static variable in App class?? Third, Please delete your previous Queries asking the same thing. - Nikhileshwar
Oh yeah, I meant btnofflinedone instead of btndonecount. What do you mean set a static value in app class? Sorry I am very new to xamarin so I am still figuring how all each functions work. - Charis
I'll write it as an answer. Easy to show code than to explain. AFAIK, You require to set the mainpagevalue only on the first navigation to SecondPage. Am I right? - Nikhileshwar
Hmm, I think is more of setting the mainpagevalue on the first click for btnofflinedone? I only want to get the time stamp on first click for both btnOffline and btnofflinedone . Right now it works for btnOffline no matter how many times I navigate back and forth before pressing btnDone in SecondPage - Charis
This is just because you are creating new instance of SecondPage every time you navigate and since btnofflinedone is not static you are checking a new variable every time you are checking in the btnDone_Clicked of SecondPage. This is why I suggested a static variable. I'll show it in Answer please wait - Nikhileshwar

1 Answers

0
votes

First and foremost as a suggestion, please use MS doc's naming guidelines, they improve your code's readability very much. You query could be easily understood and you could get help quickly.

Now, for the above issue,

You are creating a new instance of SecondPage every time you navigate from MainPage's button click in the following line of code.

Navigation.PushAsync(new SecondPage(this, lblEndDT));

And you are checking the SecondPage's non-static variable on btnDone_Clicked. Every time you create a new instance non-static variable will be re-created and reset to default. This is why you are not able to restrict the TimeStamp label update.

For quick fix- add static to btnofflinedone

static int btnofflinedone = 0;

My suggestion is to shift the static btnofflinedone variable from SecondPage to App class

App.Xaml.cs

public partial class App : Application
{
    internal static int btnofflinedone { get; set; } = 0;

    public App()
    {
    ......

Remove the btnofflinedone from SecondPage and replace btnofflinedone with App.btnofflinedone.