[英]How to use TimeSpan string format in code in Xamarin.Forms?
I am using a Label
in Xamarin.Forms in code like so:我在代码中使用 Xamarin.Forms 中的
Label
,如下所示:
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Time", stringFormat: "{}{0:hh\\:mm}", mode: BindingMode.TwoWay, source: this));
But this returns the error:但这会返回错误:
System.FormatException: Input string was not in a correct format.
System.FormatException: 输入字符串的格式不正确。
But this works in Xaml:但这在 Xaml 中有效:
<Label Text="{Binding StartTime, StringFormat='{}{0:hh\\:mm}'}}"/>
How do I use string format on a binding for TimeSpan
in Xamarin.Forms?如何在 Xamarin.Forms 中的
TimeSpan
绑定上使用字符串格式?
Xamarin Forms uses all the basic string.Format
options you would normally use. Xamarin Forms 使用您通常使用的所有基本
string.Format
选项。 So for a datetime the stringFormat
variable would look like this:因此,对于日期时间,
stringFormat
变量将如下所示:
"{0:MM/dd/yy H:mm:ss zzz}"
The additional pair of brackets in your format string seem out of place to me.格式字符串中的附加括号对我来说似乎不合适。 You could try the following for what you're trying to achieve here:
您可以尝试以下操作来实现您在此处的目标:
@"{0:hh\:mm}"
StringFormat='{}{0:hh\\\\:mm\\\\:ss}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.