如何通过ConverterParameter将TimeSpan.FromSeconds传递给转换器?

时间:2017-10-11 14:07:23

标签: c# wpf xaml converter func

鉴于以下内容:

namespace Foo.Converters {
    public class TimespanConverter : IValueConverter {
        public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) =>
            ( parameter as Func<double, TimeSpan> )?.Invoke( ( double )( value ) );
        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) =>
            throw new NotImplementedException( );
    }
}

如何将TimeSpan.FromSeconds传递给ConverterParameter

我尝试过以下但是没有用 -

<Window
    x:Class="Foo.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:c="clr-namespace:Foo.Converters"
    xmlns:lib="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Foo"
    mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <c:TimespanConverter x:Key="TSC" />
    </Window.Resources>
    <Viewbox>
        <TextBlock Text="{Binding
            Seconds, Source={x:Static Application.Current},
            Converter={StaticResource TSC},
            ConverterParameter={x:Static lib:TimeSpan.FromSeconds}}" />
    </Viewbox>
</Window>

我在设计师中遇到错误 - The member "FromSeconds" is not recognized or is not accessible。我已经检查了TimeSpan结构,它包含在System命名空间(mscorlib程序集下)中,过去曾经无数次使用.FromFOO进行转换(只是没有)以这种方式)。

我知道我可以直接在转换器中调用TimeSpan.FromSeconds,但我希望能有更灵活的东西。

我正在努力做到这一点?我怎么能正确地做到这一点?

1 个答案:

答案 0 :(得分:2)

感谢有用的评论者,错误在于实施。显然,您不能隐式地将方法或函数名称作为参数传递(至少不是来自XAML)。

为了避免这种情况,我更改了转换器代码以将TimeSpan.FromFOO函数公开为Func<double,TimeSpan>属性,以便将它们传递给ConverterParameter -

namespace Foo.Converters {
    public class TimespanConverter : IValueConverter {
        public static Func<double, TimeSpan> FromMilliseconds => TimeSpan.FromMilliseconds;
        public static Func<double, TimeSpan> FromSeconds => TimeSpan.FromSeconds;
        public static Func<double, TimeSpan> FromMinutes => TimeSpan.FromMinutes;
        public static Func<double, TimeSpan> FromHours => TimeSpan.FromHours;
        public static Func<double, TimeSpan> FromDays => TimeSpan.FromDays;

        public object Convert(
            object value, Type targetType, object parameter, CultureInfo culture ) =>
            ( parameter as Func<double, TimeSpan> )?.Invoke( ( double )( value ) );
        public object ConvertBack(
            object value, Type targetType, object parameter, CultureInfo culture ) =>
            throw new NotImplementedException( );
    }
}

窗口 -

<Window
    x:Class="Foo.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:c="clr-namespace:Foo.Converters"
    xmlns:lib="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Foo"
    mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <c:TimespanConverter x:Key="TSC" />
    </Window.Resources>
    <Viewbox>
        <TextBlock Text="{Binding
            Seconds, Source={x:Static Application.Current},
            Converter={StaticResource TSC},
            ConverterParameter={x:Static c:TimespanConverter.FromSeconds}}" />
    </Viewbox>
</Window>

设计时错误消失了,但文字仍未显示。我不太担心实际显示的文本和实现它的最佳方法,这似乎是目前最好的选择。此外,它可以扩展到任何人可能希望将Func作为ConverterParameter传递的实例。