我有一个通用类实现IValueConverter.就像是:
class MyValueConverter<T> : IValueConverter
Run Code Online (Sandbox Code Playgroud)
使用XAML 2009,我可以像这样使用它:
<my:MyValueConverter x:TypeArguments='x:String'/>
Run Code Online (Sandbox Code Playgroud)
但显然不允许"编译"的XAML(我想我们必须等待.NET 5)
我目前的解决方法是为每种用法进行子类化:
class FooMyValueConverter : MyValueConverter<Foo>
Run Code Online (Sandbox Code Playgroud)
是否可以仅使用XAML 2006在标记中执行此操作?
您可以使用自定义MarkupExtension(存档)(v4)执行此操作.就像是:
public class MyMarkupExtension : MarkupExtension {
public MyMarkupExtension() {
this.Type = /* some default type */;
}
public MyMarkupExtension(Type type) {
this.Type = type;
}
public Type Type { get; private set; }
public override object ProvideValue(IServiceProvider serviceProvider) {
Type type = typeof(MyValueConverter<>).MakeGenericType(this.Type);
return Activator.CreateInstance(type);
}
}
Run Code Online (Sandbox Code Playgroud)
那你就像使用它一样 {Binding ... Converter={local:MyMarkup {x:Type BounceEase}}}