Sto cercando di scrivere un MarkupExtension come questo:Può un TypeConverter essere utilizzato per argomento del costruttore
[MarkupExtensionReturnType(typeof(Length))]
public class LengthExtension : MarkupExtension
{
// adding the attribute like this compiles but does nothing.
public LengthExtension([TypeConverter(typeof(LengthTypeConverter))]Length value)
{
this.Value = value;
}
[ConstructorArgument("value")]
public Length Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this.Value;
}
}
Per essere utilizzato in questo modo:
<Label Content="{units:Length 1 mm}" />
sbaglia con:
Il TypeConverter per il tipo "Length" non supporta la conversione da stringa.
Il TypeConverter funziona se:
- Metterlo sulla proprietà
Value
e hanno un ctor di default. - Decorare il tipo
Length
con l'attributo.
Mentre questo può essere x/y non voglio nessuna di quelle soluzioni.
ecco il codice per il convertitore:
public class LengthTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var text = value as string;
if (text != null)
{
return Length.Parse(text, culture);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is Length && destinationType != null)
{
var length = (Length)value;
if (destinationType == typeof(string))
{
return length.ToString(culture);
}
else if (destinationType == typeof(InstanceDescriptor))
{
var factoryMethod = typeof(Length).GetMethod(nameof(Length.FromMetres), BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(double) }, null);
if (factoryMethod != null)
{
var args = new object[] { length.metres };
return new InstanceDescriptor(factoryMethod, args);
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
Grazie signore, non riesco a cercare su Google. Ti capita di sapere quanta conversione implicita applica l'attributo alla causa della definizione di classe? Forse questa è un'altra domanda. O forse [chat] (http://chat.stackoverflow.com/rooms/18165/wpf) –
Non sai cosa significhi "Quanta conversione implicita". Ma se si applica l'attributo al tipo Lunghezza, la conversione avverrà ogni volta che si imposta una lunghezza in XAML. – Clemens
Anche i collegamenti estesi lo utilizza. Forse cose come la serializzazione. Non sono un grande fan della conversione implicita :) –