【问题标题】:Xamarin forms Button click countXamarin 表单按钮点击计数
【发布时间】:2020-03-13 17:17:38
【问题描述】:

当用户点击主页上的 btnOffline 时,它​​会将他们重定向到第二页。当用户单击第二页上的按钮完成(button_clicked 事件)时,我将记录时间戳。我现在面临的错误是当它在 button_clicked 事件之后导航回主页时,我再次单击 btnOffline 和 btnDone,lblEndDT 的时间戳发生了变化。我只想从第二页获得第一次点击,但它仍然会发生变化

主页

 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;
      }


   }

第二页

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();
   }
}

【问题讨论】:

  • 我试过了,还是变了

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

当您再次单击btnOfflinebtnDone 时,SecondPage 中的btndonecount 会重置,因此它仍然会发生变化。

您应该从mainPage 获取点击计数,将offlinecount 传递给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;
    }


}

SecondPage 中,获取clickCount 并删除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();
    }

【讨论】:

  • 您好,感谢您的帮助。我试过了,但我的结束日期时间没有出现
  • 您是否删除了btndonecount++;?在MainPagelblEndDT = edt; 行添加断点,看看是否命中。
  • 哦,我忘了删除 btndonecount++ 。有效!谢谢你:))
  • 您好,抱歉。我意识到如果我设置与主页相同的计数,当我来回导航时,我在第二页完成的 btn 将不再工作
  • 所以我从主页点击 btnOffline ,它会导航到第二页,对吧?有一个后退按钮,如果我在没有点击 btnDone 的情况下继续来回导航,当我在主页和第二页之间多次导航后最终点击 btnDone 时,时间戳不会显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
相关资源
最近更新 更多