Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

I want to increase a minimum figure by 5 every 60 secs and must not exceed maximum figure in Xamarin/C#

So, I'm working on a project where I'm displaying two amounts. The first one is the minimum amount which is constant. However, the second one is the progress (increment) every 60 seconds but the figure must not exceed the maximum amount set within a certain duration. an example is: I have a min amount of 10,000. max amount = 50,000. savings duration= 93 days. now, I want the progress count (a label showing how the savings is growing) to keep increasing by a certain given time (probably 60 seconds) until the 93rd day without exceeding the maximum amount.

My question is, how do I achieve this? what method can best give a good result?

Here is my current implementation:

public string TotalBalance
    {
        get
        {
            //string newBal;
            double min;
            double max;
            double dys;
            dys = double.Parse(days);
            double calc = dys * 1440;
            min = double.Parse(amount);
            max = double.Parse(totalReturn);
            double costpermin = max / calc;
            if (dys>0)
            {
                string kems;
                Device.StartTimer(new TimeSpan(0, 0, 60), () =>
                {
                    // do something every 60 seconds
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        double pel = min + costpermin++;
                        string polo = pel.ToString();
                        string Bal = Math.Round(Convert.ToDouble(polo), 2).ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us")).Replace("$", "N");

                        kems = Bal;
                        var updAmt = Bal;
                        MessagingCenter.Send<object, string>(this, "timer", updAmt);
                    });
                    return true; // runs again, or false to stop
                });
                return kems;
            }
            else
            {
                string meeBal = Math.Round(Convert.ToDouble(this.totalReturn), 2).ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us")).Replace("$", "N");
                return meeBal;
            }
        }

        set
        {
            TotalBalance = value;
            OnPropertyChanged(nameof(TotalBalance));
        }
    }

I added deviceTimer in my model to be able to update the view real-time. however, there are some issues. 1. the calculation is right but only calculates after the first 60secs. so onAppearance, the amount label shows empty. and the view doesn't get updated still. 2. the calculation doesn't continue tomorrow, so if today your total growth shows 10,200.12, when you open the app again tomorrow, it will start the counting again instead of starting from where it started.

question from:https://stackoverflow.com/questions/65931666/i-want-to-increase-a-minimum-figure-by-5-every-60-secs-and-must-not-exceed-maxim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try using Device.StartTimer() to increment count periodically

https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.device.starttimer?view=xamarin-forms


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...