我必须将转换器与 Texblocks 和 UWP 应用程序中的滑块一起使用。使用 Binding 时一切正常,但更改为 x:Bind 后转换器停止工作。属性正在正确更新,如果我删除转换器(从文本框中),我会看到正在更新的值(但没有正确的格式)。
也许有人可以帮助我更改转换器代码以与 x:Bind 兼容(我已经尝试过,但找不到解决方案)。
属性:
Position = TimeSpan
Duration = TimeSpan
Duration.TotalSeconds = double
Slider 控件需要将 Position 转换为 double。Textblock 控件需要将 Position 转换为具有正确格式的字符串。
XAML 代码滑块控件
<Slider Name="PositionSlider" Orientation="Horizontal" Margin="10"
Value="{x:Bind ViewModel.Position, Mode=TwoWay, Converter={StaticResource TimeSpanToSecondsConverter}}"
Maximum="{x:Bind ViewModel.Duration.TotalSeconds}"/>
文本块控件
<TextBlock Margin="10" Text="{x:Bind ViewModel.Position, Converter={StaticResource TimeSpanToStringConverter}}"/>
“TimeSpanToSecondsConverter”的代码
public sealed class TimeSpanToSecondsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var timeSpan = value as TimeSpan?;
return timeSpan?.TotalSeconds ?? 0.0;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
var seconds = value as double?;
return TimeSpan.FromSeconds(seconds ?? 0);
}
}
“TimeSpanToStringConverter”的代码
public sealed class TimeSpanToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var timeSpan = value as TimeSpan?;
return timeSpan?.ToString("mm\\:ss");
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotSupportedException();
}
}
谢谢你。