我知道,对于具有指定的clr-namespace:和assembly=令牌的控件,XamlReader只是在指定的程序集中查找该类型。
但是默认名称空间xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"中的默认WPF控件呢?
我试图在一个树中获取每个元素的类型,但是当没有指定程序集时,我不知道如何找到它。
例如,以下所有示例都返回null:
Type.GetType("Grid")typeof(Control).Assembly.GetType("Grid")Assembly.GetAssembly(typeof(Control)).GetType("Grid")帮助?
发布于 2015-02-20 22:55:42
要复制XamlReader的行为,可以使用XamlSchemaContext来执行类型的查找。有关详细信息,请参阅MSDN上的默认XAML架构上下文和WPF XAML架构上下文。
GetXamlType方法允许您传递Xaml名称空间和类型名称:
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)请注意,当存在命名空间时,此技术也可以工作,它允许您有一个统一的机制来解析Xaml资源。
https://stackoverflow.com/questions/28639563
复制相似问题