[英]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?
我该怎么做才能正确?
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.