如何在Windows Phone中将值解析为字符串?

时间:2013-07-25 13:27:24

标签: c# .net windows-phone-7 xaml windows-phone-8

如何将此双格式值(距离,卡路里)解析为字符串格式?它们采用textBlock。需要在按钮clickevent中将它们转换为字符串。

private void PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {            
            var coord = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
     var calories = caloriesLabel.Text;
                var previousPoint = _line.Path.Last();
                var distance = coord.GetDistanceTo(previousPoint);
                var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
                _kilometres += distance / 1000.0;               
                paceLabel.Text = TimeSpan.FromMilliseconds(millisPerKilometer).ToString(@"mm\:ss");
                distanceLabel.Text = string.Format("{0:f2} km", _kilometres);
                caloriesLabel.Text = string.Format("{0:f0}", _kilometres * 65);
}

2 个答案:

答案 0 :(得分:0)

你可以用, 例如,

double d = 13.000;
string.Format("{0:0.000}km", d)

或者您可以将format参数传递给ToString方法。

string s = d.ToString("0.000 km");
Console.WriteLine(s);

如果“0.000”未作为参数传递,那么只会显示13个。希望它有帮助

答案 1 :(得分:0)

处理此问题的最佳方法是进行tryparse。

String Dist, Cals;

bool distParsed = Double.TryParse(distance,out Dist);
bool calsParsed = Double.TryParse(calories,out Cals);

if(!distParsed||!calseParsed)
{
  String message = !distParsed ? "Distance failed to parse" : "";
  if(message.Trim().Length==0)
    message = !calsParsed ? "Calories failed to parse" : "";
  MessageBox.Show(message);
}