请参阅下面的 VS2013 更新。
将类用作 d:DesignInstance
时暴露了 KeyedCollection<TKey, TItem>
,XAML 设计器发出以下警告:
The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation
可以使用以下简单程序重现该问题:
<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
的类提供设计时形状.
知道这里发生了什么吗?
更新:从 VS2013 开始,设计人员在处理 KeyedCollection
时的行为已更改(尽管仍未完全正常工作)。
上面的例子不再产生错误。但是,如果 KeyedCollection
使用某些类型(例如接口(interface))作为 TItem
生成以下错误:
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”这样的前缀来解决这个问题:
d:DataContext="{d:DesignInstance d:Type=local:MyClass}"
这似乎是 VS2013 设计器中的错误。我相信 d:Type 属性应该是 d:DesignInstance
属性的默认属性。另外,奇怪的是,我只在 KeyedCollection
类中看到过这个问题。
此外,MSDN examples d:DesignInstance
用法有时使用不带前缀的“Type”。在此示例中,如果省略 d:Type
或缺少前缀,则会生成设计时错误,如我所提到的。
关于c# - KeyedCollection 和 d :DataContext Design Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030381/