ValueConversionAttribute类的作用是什么?

48

这个属性有什么意义?我添加了它之后仍然需要对值对象进行转换。

[ValueConversion(sourceType: typeof(double), targetType: typeof(string))]
public class SpeedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var speed = (double)value;

这只是为了代码可读性吗? 因为当我在xaml中将绑定的路径更改为字符串时,Visual Studio没有警告不正确类型并且异常仅在转换时抛出,因此即使在编译期早期的错误捕获中也没有意义。 我也可以将强制转换更改为字符串,没有任何警告被引发,尽管它与此属性冲突。

2个回答

36

你可以使用ValueConversionAttribute来确定转换器中涉及的类型,并且可以有用地使用该信息。 请查看在WPF中使用管道值转换器,这是一个关于使用ValueConversionAttribute的绝佳示例。

示例展示了如何链接多个转换器类,并且可以使用ValueConversion将类型信息传递给下一个转换器。

[ValueConversion( typeof( string ), typeof( ProcessingState ) )]
public class IntegerStringToProcessingStateConverter : IValueConverter
{
 object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  int state;
  bool numeric = Int32.TryParse( value as string, out state );
  Debug.Assert( numeric, "value should be a String which contains a number" );
  Debug.Assert( targetType.IsAssignableFrom( typeof( ProcessingState ) ), 
    "targetType should be ProcessingState" ); 

  switch( state )
  {
   case -1:
    return ProcessingState.Complete; 
   case 0:
    return ProcessingState.Pending; 
   case +1:
    return ProcessingState.Active;
  }
  return ProcessingState.Unknown;
 } 

 object IValueConverter.ConvertBack( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  throw new NotSupportedException( "ConvertBack not supported." );
 }
}
// *************************************************************
[ValueConversion( typeof( ProcessingState ), typeof( Color ) )]
public class ProcessingStateToColorConverter : IValueConverter
{
 object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  Debug.Assert(value is ProcessingState, "value should be a ProcessingState");
  Debug.Assert( targetType == typeof( Color ), "targetType should be Color" );

  switch( (ProcessingState)value )
  {
   case ProcessingState.Pending:
    return Colors.Red; 
   case ProcessingState.Complete:
    return Colors.Gold; 
   case ProcessingState.Active:
    return Colors.Green;
  }
  return Colors.Transparent;
 } 

 object IValueConverter.ConvertBack( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  throw new NotSupportedException( "ConvertBack not supported." );
 }
} 

object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture )
{
 object output = value; 
 for( int i = 0; i < this.Converters.Count; ++i )
 {
  IValueConverter converter = this.Converters[i];
  Type currentTargetType = this.GetTargetType( i, targetType, true );
  output = converter.Convert( output, currentTargetType, parameter, culture );

  // If the converter returns 'DoNothing' 
  // then the binding operation should terminate.
  if( output == Binding.DoNothing )
   break;
 } 
 return output;
}
//***********Usage in XAML*************
    <!-- Converts the Status attribute text to a Color -->
    <local:ValueConverterGroup x:Key="statusForegroundGroup">
          <local:IntegerStringToProcessingStateConverter  />
          <local:ProcessingStateToColorConverter />
    </local:ValueConverterGroup>

2
在我看来,这应该是最佳答案,因为它展示了属性的编程需求和用途。很好的发现。 - Mark A. Donohoe

16

这只是一个注解。

MSDN:

实现 IValueConverter 接口时,使用 ValueConversionAttribute 特性对实现进行装饰是一种良好的实践,以指示开发工具所涉及的转换数据类型。

我不知道“开发工具”会如何利用这些信息......


在注释中编写这样的信息会更容易。也许它用于自动生成文档? - Lars
10
嗯,至少我在生活中有一个目的——编写一个Visual Studio插件来使用这个属性。 - Lars
3
如果这段文字最初来自于那个链接的页面,那么它现在已经被更改了。该描述现在仅说明“表示允许值转换器的作者指定转换器实现中涉及的数据类型的属性”。现在甚至更不具有帮助性了! - Sheridan
@Sheridan:确实。这是版本4的示例部分(仍可通过“其他版本”下拉菜单访问)。 - H.B.
1
啊,你说得太对了。他们从最新的页面中删除示例部分似乎很奇怪。 - Sheridan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接