简体   繁体   中英

Xamarin Forms entry text doesn't return a number

I'm very new to programming. I'm trying to store entry text in float variables, then calculate basic percentage and show it below inside a label. In place of a variable inside a label it prints NaN. If I use integers instead it says that I'm trying to divide by zero which tells me that the text read from entry returns nothing.

What could be the reason for that?

public partial class GoalTrackerPage : ContentPage
{
    float goal = 0.0000f;
    float done = 0.0000f;
    float progress = 0.0000f;


    public GoalTrackerPage ()
    {
        InitializeComponent ();

        g1EntryGoal = new Entry();
        g1EntryDone = new Entry();
        g1PrBar = new ProgressBar();          
    }

    private void add1_Clicked(object sender, EventArgs e)
    {
        GoalStatusLabelView();
    }

    private void GoalStatusLabelView ()
    {          
        progress = done / goal * 100.0000f;
        g1StatusLabel.Text = "The Goal is at " + progress;
    }

    private void g1EntryGoal_Completed(object sender, EventArgs e)
    {
        goal = float.Parse(g1EntryGoal.Text ?? "0");
    }

    private void g1EntryDone_Completed(object sender, EventArgs e)
    {
        done = float.Parse(g1EntryDone.Text ?? "0");
    }
{          
    progress = done / goal * 100.0000f;
    g1StatusLabel.Text = "The Goal is at " + progress;
}

The result is basically just zero, and since it is carrying a double value, it will return NaN at g1StatusLabel.Text.

Perhaps this could shed a little more info on dealing NaN result.

Double.NaN Field

I figured it out! Since I've been assigning text property inside the event "Text Completed" I should've used different syntax. The proper syntax for this case would be:

var g1DoneText = ((Entry)sender).Text;

instead of standard: var g1DoneText = G1EntryDone.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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