[英]Xamarin Forms entry text doesn't return a number
我是编程新手。 我正在尝试将输入文本存储在float变量中,然后计算基本百分比并将其显示在标签下方。 代替标签内的变量,它显示NaN。 如果我改用整数,它表示我正在尝试除以零,这表明从条目读取的文本不返回任何内容。
可能是什么原因呢?
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;
}
结果基本上只是零,并且由于它携带一个双精度值,它将在g1StatusLabel.Text返回NaN。
也许这可以为处理NaN结果提供更多的信息。
我想到了! 由于我一直在事件“ Text Completed”中分配text属性,因此我应该使用其他语法。 这种情况的正确语法为:
var g1DoneText =((Entry)sender).Text;
而不是标准:var g1DoneText = G1EntryDone.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.