[英]Xamarin forms Button click count
當用戶點擊主頁上的 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();
}
}
當您再次單擊btnOffline
和btnDone
時, 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.