ValueConversionAttribute

ValueConversionAttribute

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

[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中将绑定(bind)的路径更改为String时,Visual Studio不会给出有关错误类型的警告,并且仅在强制转换时才会抛出异常,因此即使在编译时早期捕获错误中也没有任何意义。
我还可以将强制类型更改为字符串,并且即使与此属性发生冲突也不会引发警告。

最佳答案

您可以潜在地使用ValueConversionAttribute来确定转换器涉及的类型,并有用地使用该信息。
Piping Value Converters in 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>

关于c# - ValueConversionAttribute类的重点是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9628859/

10-13 06:05