我对设计器有一个简单的问题。我正在尝试将DataGridComboBoxColumn的ItemsSource绑定到Enum值列表。它工作正常,代码编译和执行就好了。但是,设计师说“问题加载”并且无法正确加载。如果我单击“重新加载设计器”,它会在错误列表中显示错误。我正在使用VS2010。
<ObjectDataProvider x:Key="myEnum"
MethodName="GetValues"
ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="data:ParentClass+MyEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
这在应用程序执行时工作正常。然而,在设计师中它说:
错误5输入'数据:未找到ParentClass + MyEnum'。
我不确定我在哪里遇到过XAML的Class + Subclass语法(而不是Class.Subclass),或者为什么它是必要的,但如果代码有效,设计师应该会工作?这会占用我整个窗口的所有设计时支持,如果我想看看设计时的变化是什么,那就不好了
更新 好的,还有一些信息:首先,+语法来自Type.GetType(String) method,您可以在那里看到它的格式。
但是,System.Windows.Markup.TypeExtension使用IXamlTypeResolver服务来解析该类型。从反射器,我们可以看到:
IXamlTypeResolver service = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
this._type = service.Resolve(this._typeName);
根据我的理解,设计师使用与运行时完全不同的服务实现?!我没有找到实现。
我相信我可以编写自己的“TypeExtension”类,只返回Type.GetType(typeName)。如果这只是一个错误或一种让它发挥作用的方法,我仍然很好奇。
UPDATE2
我创建了自己的TypeExtension类,但没有帮助。由于某种原因,Type.GetType()无法通过设计器(但不是运行时)解析我的Enum
public class CreateTypeExtension : TypeExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.Type == null)
{
if (this.TypeName == null)
{
throw new InvalidOperationException();
}
this.Type = Type.GetType(TypeName);
if (this.Type == null)
{
throw new InvalidOperationException("Bad type name");
}
}
return this.Type;
}
}
我传递了它
<comm:CreateType TypeName="Company.Product.Class+Enum,Company.Assembly" />
同样,在运行时工作并且完全合格但它在设计时不起作用。
答案 0 :(得分:1)
好吧,我意识到,当它在 Update2 之后无效时,错误信息比我投掷的更具体。因此,它没有使用我的覆盖。我将其修改为不扩展TypeExtension而改为MarkupExtension,现在它可以工作。
public class CreateTypeExtension : MarkupExtension
{
public Type Type { get; set; }
public String TypeName { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.Type == null)
{
if (this.TypeName == null)
{
throw new InvalidOperationException();
}
this.Type = Type.GetType(TypeName);
if (this.Type == null)
{
throw new InvalidOperationException("Bad type name");
}
}
return this.Type;
}
}
您可以使用Type.GetType文档中提供的任何格式,但您不能再使用XAML前缀(除非您自己实现)
<comm:CreateType TypeName="Company.Product.Class+Enum,Company.Assembly" />