gpt4 book ai didi

wpf - XAML 子类类型声明设计器错误

转载 作者:行者123 更新时间:2023-12-04 15:47:40 24 4
gpt4 key购买 nike

我对设计器有一个简单的问题。我正在尝试将 DataGridComboBoxColumn 的 ItemsSource 绑定(bind)到枚举值列表。它有效,代码编译和执行都很好。然而,设计者说“Problem loading”并且不会正确加载。如果我单击“重新加载设计器”,它会在错误列表中显示错误。我正在使用 VS2010。

<ObjectDataProvider x:Key="myEnum"
MethodName="GetValues"
ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="data:ParentClass+MyEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

这在应用程序执行时工作正常。但是,在设计器中它说:

Error 5 Type 'data:ParentClass+MyEnum' was not found.

我不确定我在哪里遇到过 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)。我仍然很好奇这只是一个错误还是一种让它起作用的方法。

更新2

我创建了自己的 TypeExtension 类,但没有帮助。由于某种原因,Type.GetType() 无法通过设计器(但不是运行时)解析我的枚举

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" />

同样,在运行时工作并且完全合格但在设计时不工作。

最佳答案

好吧,我意识到当它在 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" />

关于wpf - XAML 子类类型声明设计器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com