对象与 DesignData 中的目标类型不匹配 [英] Object does not match target type in DesignData

查看:26
本文介绍了对象与 DesignData 中的目标类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人以前遇到过这种情况,我将其丢弃.

I'm throwing this out in case someone has encountered this before.

在创建用于 WPF 设计器的 DesignData 时,出现以下两个错误之一:

When creating DesignData for use within the WPF designer, I get one of two errors:

对象与目标类型不匹配.
在 System.Reflection.RuntimeMethodInfo.CheckConsistency(Object目标)
(片段)
在 Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.更新属性(IInstanceBuilderContext上下文、视图节点、视图节点、IPropertypropertyKey, DocumentNode valueNode)

Object does not match target type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
(SNIP)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder. UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode)

另一个信息更丰富:

值_.di0.MyProjectLol.MyType"不是MyProjectLol.MyType"类型并且不能在这个泛型中使用集合.
在System.ThrowHelper.ThrowWrongValueTypeArgumentException(对象值,类型 targetType)
(片段)
在 Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateChildren(IInstanceBuilderContext)上下文,视图节点视图节点,DocumentCompositeNode复合节点,布尔值 isNewInstance)

The value "_.di0.MyProjectLol.MyType" is not of type "MyProjectLol.MyType" and cannot be used in this generic collection.
at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType)
(SNIP)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder. InstantiateChildren(IInstanceBuilderContext context, ViewNode viewNode, DocumentCompositeNode compositeNode, Boolean isNewInstance)

在调试时,我可以看到有一个动态程序集加载了代理类型,看起来与我的类似,但显然不是.此程序集称为 Blend_RuntimeGeneratedTypeAssembly(此处为 Guid).当尝试加载此程序集中的类型时,它会为其中的一些引发类型加载异常.所以,有些类型被代理了,有些类型被保留为上帝和我的意图,当它们混合时就会发生不自然的行为.

When debugging, I can see that there is a dynamic assembly that is loaded with proxy-ish types that look like mine but, obviously, are not. This assembly is called Blend_RuntimeGeneratedTypeAssembly(Guid goes here). When attempting to load types in this assembly, it throws a type load exception for a number of them. So, some types get proxied, some types are left as God and I intended, and when they mix unnatural acts occur.

例如,类型Foo"可能会创建一个代理,但其他类型不会这样做(TypeLoadExceptions).然后,设计者尝试将我的一种真实类型交给代理(xaml 序列化喜欢将集合转换为 IList,因此在类型安全方面很糟糕),您会得到上述例外之一.

For example, the type "Foo" might get a proxy created, but no other types do (TypeLoadExceptions). Then the designer tries to hand one of my real types the proxy (helped by the fact that xaml serialization likes to cast collections to IList, thus shitting on type safety) and you get one of the above exceptions.

我花了半个星期试图解决这个问题.我已经尝试了一百种不同的方法,但我无法弄清楚究竟是什么导致它失败.欢迎提出建议,TIA.

I have spent half a week trying to fix this. I've tried a hundred different things, but I can't figure out exactly what is causing it to fail. Suggestions are welcome, TIA.

推荐答案

解决方案分两部分:

1) 确保 VS 已完全更新.此时,这意味着安装 Visual Studio 2010 的 Silverlight 4 工具.它们包括对WPF 设计师.如果你在遥远的未来读到这篇文章,请忽略这个.

1) Ensure VS is fully updated. At this point, it means installing the Silverlight 4 tools for Visual Studio 2010. They include the latest updates to the WPF designer. If you're reading this off in the distant future, ignore this one.

2) 点击设计数据文件的属性.清除自定义工具",并将构建操作"设置为DesignData"或DesignTimeDataWithDesignTimeCreatableTypes".

2) Hit the properties for your design data files. Clear out "Custom Tool", and set the "Build Action" to either "DesignData" or "DesignTimeDataWithDesignTimeCreatableTypes".

DesignData 意味着您的类型不能直接从 xaml 反序列化(由于依赖关系等),因此设计人员尝试为这些类型创建模拟并将模拟呈现给您的设计界面.

DesignData means that your types cannot be deserialized from xaml directly (due to dependencies or the such), so the designer attempts to create mocks for those types and presents the mocks to your design surface.

DesignTimeDataWithDesignTimeCreatableTypes 意味着设计器将加载您的程序集并将 xaml 直接反序列化为您的类型,而无需创建模拟.

DesignTimeDataWithDesignTimeCreatableTypes means that the designer will load your assemblies and deserialize the xaml directly into your types without creating mocks.

这篇关于对象与 DesignData 中的目标类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆