0
votes

When user click on btnOffline on Main page, it will redirect them to second page. I will record the time stamp when user click on button done (button_clicked event) on second Page. The error I am facing right now is when it navigates back to main page after button_clicked event, I click on btnOffline and btnDone again, the timestamp for lblEndDT changes. I only want to get the first click from second page but it still changes

Main Page

 public partial class MainPage : ContentPage
{
public string mainpagevalue;

int offlinecount = 0;
int onlinecount = 0;

public MainPage()
{
    InitializeComponent();

}
private void btnOffline_Clicked(object sender, EventArgs e)
{
    offlinecount++;
    txtOfflineStatus.Text = "IN PROGRESS";

    Navigation.PushAsync(new SecondPage(this, lblEndDT, txtOfflineStatus, btnOnline, btnMH));


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


   }

Second Page

public partial class SecondPage : ContentPage
{
    Label MainPagelblEndDT;
    MainPage mainPage;
    ImageButton myImageBtn;
    int btndonecount = 0;
public SecondPage()
{
    InitializeComponent();
}

public SecondPage(MainPage mainP,Label lblEndDT)
{
    InitializeComponent();

    //Get the lblEndDT reference here
    MainPagelblEndDT = lblEndDT;
    //Get the MainPage reference here
    mainPage = mainP;
}

public SecondPage(MainPage mainP, Label lblEndDT, ImageButton imageBtn)
{
    InitializeComponent();

    //Get the lblEndDT reference here
    MainPagelblEndDT = lblEndDT;
    //Get the MainPage reference here
    mainPage = mainP;
    //Get the ImageButton reference here
    myImageBtn = imageBtn;
}

private void Button_Clicked(object sender, EventArgs e)
{           
    btndonecount++;
    if(btndonecount == 1)
    {

       string edt = DateTime.Now.ToString();
       MainPagelblEndDT = edt;
       mainPage.mainpagevalue = MainPagelblEndDT.Text;
    }

       Navigation.PopAsync();
   }
}
1
I tried but it still changed - Charis

1 Answers

0
votes

When you click on btnOffline and btnDone again, the btndonecount in SecondPage reset, so it still changes.

You should get the click count from mainPage, pass the offlinecount to SecondPage:

private void btnOffline_Clicked(object sender, EventArgs e)
{
    offlinecount++;
    txtOfflineStatus.Text = "IN PROGRESS";

    Navigation.PushAsync(new SecondPage(this, lblEndDT, txtOfflineStatus, btnOnline, btnMH,offlinecount));


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


}

In the SecondPage, get the clickCount and remove the btndonecount++:

   public SecondPage(MainPage mainP, Label lblEndDT, ImageButton imageBtn, int clickCount)
    {
        InitializeComponent();

        //Get the lblEndDT reference here
        MainPagelblEndDT = lblEndDT;
        //Get the MainPage reference here
        mainPage = mainP;
        //Get the ImageButton reference here
        myImageBtn = imageBtn;

        btndonecount = clickCount;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        if (btndonecount == 1)
        {
            string edt = DateTime.Now.ToString();
            MainPagelblEndDT = edt;
            mainPage.mainpagevalue = MainPagelblEndDT.Text;
        }

        Navigation.PopAsync();
    }