首页 > 解决方案 > Xamarin Forms 条目文本不返回数字

问题描述

我对编程很陌生。我正在尝试将条目文本存储在浮点变量中,然后计算基本百分比并将其显示在标签下方。代替标签内的变量,它打印 NaN。如果我改用整数,它表示我正在尝试除以零,这告诉我从 entry 读取的文本不返回任何内容。

这可能是什么原因?

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

标签: c#formsxamarinmathtext

解决方案


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

结果基本上为零,并且由于它带有双精度值,因此它将在 g1StatusLabel.Text 处返回 NaN。

也许这可以为处理 NaN 结果提供更多信息。

Double.NaN 字段


推荐阅读