gpt4 book ai didi

c# - ValueConversionAttribute 类的重点是什么?

转载 作者:太空狗 更新时间:2023-10-29 23:51:33 27 4
gpt4 key购买 nike

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

[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/17926226/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com