gpt4 book ai didi

c# - 如何在 Xamarin.Forms 的代码中使用 TimeSpan 字符串格式?

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:47 25 4
gpt4 key购买 nike

我在 Xamarin.Forms 中使用 Label 的代码如下:

var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Time", stringFormat: "{}{0:hh\\:mm}", mode: BindingMode.TwoWay, source: this));

但这会返回错误:

System.FormatException: Input string was not in a correct format.

但这在 Xaml 中有效:

 <Label Text="{Binding StartTime, StringFormat='{}{0:hh\\:mm}'}}"/>

如何在 Xamarin.Forms 中为 TimeSpan 的绑定(bind)使用字符串格式?

最佳答案

Xamarin Forms 使用您通常会使用的所有基本 string.Format 选项。所以对于日期时间,stringFormat 变量将如下所示:

"{0:MM/dd/yy H:mm:ss zzz}"

格式字符串中的额外一对括号对我来说似乎不合适。您可以尝试以下方法来实现您在这里想要实现的目标:

@"{0:hh\:mm}"

关于c# - 如何在 Xamarin.Forms 的代码中使用 TimeSpan 字符串格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com