I know that for controls with specified clr-namespace:
and assembly=
tokens, XamlReader just looks for that type in the specified assembly.
But what about default WPF controls in the default namespace xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
?
I'm trying to get the Type of each element in an XElement tree, but I don't know how to find it when no assembly is specified?
For example, all of the following examples return null:
Type.GetType("Grid")
typeof(Control).Assembly.GetType("Grid")
Assembly.GetAssembly(typeof(Control)).GetType("Grid")
Help?
To duplicate the behavior of XamlReader
, you can use a XamlSchemaContext
to perform the lookup of the type. For details, refer to Default XAML Schema Context and WPF XAML Schema Context on MSDN.
The GetXamlType
method allows you to pass a Xaml Namespace and type name:
var context = new XamlSchemaContext();
var xtype = context.GetXamlType(new XamlTypeName("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Grid"));
var gridType = xtype.UnderlyingType;
// gridType == typeof(System.Windows.Controls.Grid)
Note that this technique also works when there is a namespace, which allows you to have a single unified mechanism to parse your Xaml resources.