c# - 在 Xaml (UWP) 中格式化 TimeSpan
问题描述
如何TimeSpan
使用自定义格式格式化 XAML 中的 a?我想要小时和分钟。
根据官方文档,在 C# 中执行此操作的方法似乎应该是:
interval.ToString(@"h\:mm");
但是,我希望能够TimeSpan
从绑定中格式化 XAML。这个解决方案似乎可行,但我想创建一个通用转换器,我可以将格式字符串传递给它。我的转换器如下:
public class TimeSpanFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
string result = "";
if (value == null)
{
return null;
}
if (parameter == null)
{
return value;
}
if (value is TimeSpan timeSpan)
{
try
{
result = timeSpan.ToString((string)parameter);
}
catch (Exception e)
{
result = "";
}
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
理论上,我可以按如下方式使用此转换器:
<Page.Resources>
<converters:TimeSpanFormatConverter x:key="TimeSpanConverter"></converters:TimeSpanFormatConverter>
</Page.Resources>
<Grid>
<!-- Some properties omitted for brevity. -->
<ListView>
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:MyModel">
<Grid>
<!-- PROBLEM IS HERE -->
<TextBlock Text="{x:Bind Interval, Converter={StaticResource TimeSpanConverter}, ConverterParameter='h\:mm'}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
请注意,“MyModel”有一个名为“Interval”的属性,其类型为“TimeSpan”。
但是,这不起作用,因为我需要反斜杠。XAML 解析删除了反斜杠,从而将“h:mm”传递给转换器(我通过调试器进行了验证)。
它也不喜欢两个反斜杠,因为这会从生成的 .g.cs 文件中引发编译器错误,说“\:”是“无法识别的转义序列”。
没有任何编码反斜杠的变体起作用。我努力了:
h:mm
h\:mm
h\\:mm
h\\\:mm
h\:mm
h\\:mm
h\\:mm
h\\:mm
需要放入的神奇字母串是ConverterParameter
什么?
作为替代方案,此处解释的 MultiBinding 解决方案看起来很有希望,但根据 Visual Studio,UWP 不支持 MultiBinding。
解决方案
因为我需要反斜杠。XAML 解析删除了反斜杠,从而将“h:mm”传递给转换器(我通过调试器进行了验证)。
是的,是正确的,ConverterParameter
是对象而不是字符串,这可能会导致在 xaml 解析时删除反斜杠。我认为您可以StringFormat
为您创建属性并在初始化时TimeSpanFormatConverter
传递。Format
Converter
public class TimeSpanFormatConverter : IValueConverter
{
public string StringFormat { get; set; }
public object Convert(object value, Type targetType, object parameter, string language)
{
string result = "";
if (value == null)
{
return null;
}
if (parameter == null)
{
return value;
}
if (value is TimeSpan timeSpan)
{
try
{
result = timeSpan.ToString(StringFormat);
}
catch (Exception e)
{
result = "";
}
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
用法
<Page.Resources>
<local:TimeSpanFormatConverter x:Key="TimeSpanConverter" StringFormat="h\:mm"/>
</Page.Resources>
<Grid>
<TextBlock VerticalAlignment="Center" Text="{x:Bind Interval, Converter={StaticResource TimeSpanConverter},Mode=TwoWay}"></TextBlock>
</Grid>
推荐阅读
- c - 在标头中使用私有函数作为c中的#define
- sql - 在 oracle 中修改表上的 CHECK 约束的最佳方法是什么?
- typescript - 如何在使用 Puppeteer 时录制音频?
- spring-boot - 从 application.properties 调试一个不满意的绑定作为 spring-boot 启动?
- python - 如何通过不同的 csv 文件迭代选择的操作并将结果输出到新的 csv 文件
- svg - 规模
SVG中的元素 - .htaccess - 我在 .htaccess 上哪里弄错了
- javascript - 除非我重新初始化数组,否则为什么 Vue 转换无法识别所用数组的变化?
- python-3.x - python在尝试写入yaml文件时抛出NoneType错误
- c# - 'Autoclicker' 是命名空间,但用作类型