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();
}
}
}
btndonecountresets every time I navigate back and forth" do you meanbtnofflinedone. because I don't find anybtndonecount. Secondly, Why not set a static variable in App class?? Third, Please delete your previous Queries asking the same thing. - Nikhileshwarmainpagevalueonly on the first navigation toSecondPage. Am I right? - Nikhileshwarmainpagevalueon the first click forbtnofflinedone? I only want to get the time stamp on first click for bothbtnOfflineandbtnofflinedone. Right now it works forbtnOfflineno matter how many times I navigate back and forth before pressingbtnDoneinSecondPage- CharisSecondPageevery time you navigate and sincebtnofflinedoneis not static you are checking a new variable every time you are checking in thebtnDone_ClickedofSecondPage. This is why I suggested astaticvariable. I'll show it in Answer please wait - Nikhileshwar