简体   繁体   English

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

[英]How can I pass TimeSpan.FromSeconds to a converter via the ConverterParameter?

Given the following : 鉴于以下内容:

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( );
    }
}

How can I pass TimeSpan.FromSeconds to the ConverterParameter ? 如何将TimeSpan.FromSeconds传递给ConverterParameter

I've tried the following but it did not work - 我尝试过以下但是没有用 -

<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>

I get the error in designer - The member "FromSeconds" is not recognized or is not accessible . 我在设计器中收到错误 - The member "FromSeconds" is not recognized or is not accessible I've checked the TimeSpan struct and it is contained within the System namespace ( under the mscorlib assembly ), and I've used .FromFOO countless times for conversions in the past ( just not in this fashion ). 我已经检查了TimeSpan结构,它包含在System命名空间中(在mscorlib程序集下),我曾经无数次使用.FromFOO进行转换(只是不是这种方式)。

I do know that I could just call TimeSpan.FromSeconds directly in the converter, but I was hoping to have something more flexible. 我知道我可以直接在转换器中调用TimeSpan.FromSeconds ,但我希望能有更灵活的东西。

So is what I'm trying to do possible? 那么我正在尝试做什么呢? How can I do this correctly? 我该怎么做才能正确?

SOLUTION

With thanks to a helpful commenter, the error was in the implementation. 感谢有用的评论者,错误在于实施。 Evidently, you cannot implicitly pass method or function names as arguments ( at least, not in from XAML ). 显然,您不能隐式地将方法或函数名称作为参数传递(至少不是从XAML中传递)。

To circumvent this, I have changed the converter code to expose the TimeSpan.FromFOO functions as Func<double,TimeSpan> properties so that they can be passed to the ConverterParameter - 为了避免这种情况,我更改了转换器代码,将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( );
    }
}

And the window - 和窗口 -

<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>

The design-time error has gone away, but the text still doesn't show. 设计时错误已经消失,但文本仍未显示。 I'm less worried about the text actually showing and the best method for implementing this, and this seems to be the best option for now. 我不太担心实际显示的文本和实现它的最佳方法,这似乎是目前最好的选择。 Additionally, it can be extended to any instance where one may wish to pass a Func as a ConverterParameter. 此外,它可以扩展到任何人可能希望将Func作为ConverterParameter传递的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Timespan(0,0,secs)或Timespan.FromSeconds(secs) - Timespan(0,0,secs) or Timespan.FromSeconds(secs) TimeSpan.FromSeconds 给出指定参数的错误超出有效值范围。参数名称:间隔 - TimeSpan.FromSeconds gives error of Specified argument was out of the range of valid values.Parameter name: Interval 输入字符串的格式不正确。 在改变文化的IE中调用TimeSpan.FromSeconds(Convert.ToDouble(time)) - Input string was not in a correct format. calling TimeSpan.FromSeconds(Convert.ToDouble(time)) in IE on Changing Culture 在 C# 中,Timespan.FromSeconds(-1) 用于比较时意味着什么? - In C#, what would a Timespan.FromSeconds(-1) mean when used in a comparison? 如何将带空格的字符串传递给converterParameter? - How do I pass string with spaces to converterParameter? 如何将GridView作为ConverterParameter传递 - How to pass GridView as a ConverterParameter 如何在XAML中将FormatString作为ConverterParameter传递 - How to pass a FormatString as ConverterParameter in XAML 如何在WPF中将变量作为Converterparameter传递 - How to pass a variable as Converterparameter in WPF 如何在WPF中将接口类型作为ConverterParameter传递 - How to pass an interface type as ConverterParameter in WPF 如何在UWP中将静态资源字符串传递给ConverterParameter - How to pass Static Resource String to ConverterParameter in UWP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM