KeyedCollection和d:DataContext设计错误 [英] KeyedCollection and d:DataContext Design Error
问题描述
请参见下面针对VS2013的更新。
See the update below for VS2013.
将类用作 d:DesignInstance
时,它公开了一个 KeyedCollection< TKey,TItem>
,XAML设计器抱怨以下警告:
When using a class as a d:DesignInstance
that exposes a KeyedCollection<TKey, TItem>
, the XAML designer complains with the following warning:
提供的泛型参数数量不等于
泛型类型定义的数量。
The number of generic arguments provided doesn't equal the arity of the generic type definition.
参数名称:实例化
可以使用以下简单程序重现该问题:
The problem can be reproduced with the following simple program:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:MyClass}" />
namespace Test
{
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }
}
public class MyClass
{
public KeyedCollection<string, object> SettingsModule { get; private set; }
}
}
我无法提供任何公开了 KeyedCollection
的类。
I'm unable to provide design time shape with any class that exposes a KeyedCollection
.
有什么想法吗?
更新:自VS2013起,设计人员处理 KeyedCollection
的行为已更改(
Update: As of VS2013 the behavior of the designer in dealing with a KeyedCollection
has changed (though still not fully working).
上面的示例不再产生错误。但是,如果 KeyedCollection
使用某些类型(例如接口)作为 TItem
,则会产生以下错误:
The above example no longer generates an error. However, if the KeyedCollection
uses certain types (such as an interface) as the TItem
the following error is generated:
对象引用未设置为对象的实例。
Object reference not set to an instance of an object.
请考虑以下示例:
namespace Test
{
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }
}
public class MyClass
{
public KeyedCollection<string, IInterface> MyCollection { get; private set; }
}
public interface IInterface
{
string Name { get; set; }
}
}
推荐答案
我已经可以通过在设计实例类型前面加上 d:Type来解决此问题:
I've been able to resolve this issue by prefixing the design instance type with "d:Type" as such:
d:DataContext="{d:DesignInstance d:Type=local:MyClass}"
这似乎是一个VS2013设计器中的错误。我相信d:Type属性应该是 d:DesignInstance
属性的默认属性。另外,奇怪的是,我只在 KeyedCollection
类中看到过此问题。
This seems to be a bug in the VS2013 designer. I believe the d:Type property should be the default property of the d:DesignInstance
attribute. Also, strangely I've only seen this issue with the KeyedCollection
class.
此外, d:DesignInstance $ c的http://msdn.microsoft.com/en-us/library/ff602277%28v=vs.95%29.aspx\">MSDN示例 $ c>用法有时使用不带前缀的类型。在此示例中,如果省略了 d:Type
或缺少前缀,则会生成设计时错误,如我所述。
Furthermore, the MSDN examples of d:DesignInstance
usage sometimes use "Type" with no prefix. In this example if d:Type
is omitted or the prefix is missing, the design time error is generated as I mentioned.
这篇关于KeyedCollection和d:DataContext设计错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!