Resolve generic types with IXamlTypeResolver
I'd written a new TypeExtension class that replace the default System.Windows.Markup.TypeExtension to handle generic types. It allows the Xaml to work with generic types in many different ways, like :
<DataTemplate DataType="{ck:Type [here a generic type name]}">
This is how it works :
- I'd written TypeExtention class, that inherit of MarkupExtension (it's the ck:Type in the example)
I'd override the ProvideValue method to call Resolve on IXamlTypeResolver
IXamlTypeResolver service = p.GetService( true );
_type = service.Resolve( _typeName );
So, in .NET 3.5, I can resolve types li开发者_StackOverflow中文版ke "local:IConfigItemProperty`1". But now in .NET 4 (with WPF 4) the resolve method throws an exception :
Character '
' was unexpected in string 'local:IConfigItemProperty
1'. Invalid XAML type name.
Do you think .NET 4.0 does not support '`' anymore ?
While the post is a bit old, i put the answer for any other dev looking for solution. It's seem from 4.0 they change the way the Reader deal with generic. The lack of support to '`' by the IXamlTypeResolver service seem to be a bug while the regression is not documented at all. If u want to use generic, you might want to switch with x:TypeArguments which is list the argument for the generic.
C#
public class GraphBase<N,L,P> : IGraph<N,L,P>
{
...
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<GraphBase
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:MyGeoLive.Topology;assembly=MyGeoLive.Topology"
xmlns:System="clr-namespace:System;assembly=mscorlib"
x:TypeArguments="System:String,System:String,System:String" >
</GraphBase>
精彩评论