简体   繁体   English

Xamarin 表单按钮计数点击带导航

[英]Xamarin forms button count click with navigation

I debugged my own code and realize my btnofflinecount resets every time I navigate back and forth from main page to second page.我调试了自己的代码,并意识到每次从主页来回导航到第二页时,我的 btnofflinecount 都会重置。 Is it possible to not reset the counter after reload?重新加载后是否可以不重置计数器?

First, user will click btnOffline, which will redirect user to second page.首先,用户将单击 btnOffline,这会将用户重定向到第二页。 Once user click on btnDone, it will go back to main page.一旦用户单击 btnDone,它将返回主页面。 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.现在,即使我有 if 语句来检查条件,我的结束日期时间戳也会不断更新。 (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. (我将在 btnDone 事件上获得结束日期时间戳)我发现我的计数器在从第二页导航回主页时重置,但尚未触发 btnDone_Clicked。 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();


       }

     }
   } 

First and foremost as a suggestion, please use MS doc's naming guidelines , they improve your code's readability very much.首先,作为建议,请使用MS doc 的命名指南,它们极大地提高了代码的可读性。 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.每次从MainPage的按钮单击以下代码行时,您都在创建SecondPage的新实例。

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

And you are checking the SecondPage 's non-static variable on btnDone_Clicked .并且您正在btnDone_Clicked上检查SecondPage的非静态变量。 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.这就是您无法限制 TimeStamp 标签更新的原因。

For quick fix- add static to btnofflinedone为了快速修复 - 将静态添加到btnofflinedone

static int btnofflinedone = 0;

My suggestion is to shift the static btnofflinedone variable from SecondPage to App class我的建议是将静态btnofflinedone变量从SecondPage转移到App

App.Xaml.cs应用程序.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 .SecondPage删除btnofflinedone并将btnofflinedone替换为App.btnofflinedone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM