c# - KeyedCollection 和 d :DataContext Design Error

标签 c# wpf visual-studio-2013 datacontext design-time

请参阅下面的 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/

相关文章:

c# - 查找具有指定首字母的单词(正则表达式)

WPF - 将复选框复选标记更改为图像(或隐藏)

c# - WPF 如何从 ViewModel 访问控件

c++ - 如何在PCL获取相机参数?

c# - 调试时无法加载文件或程序集错误,但程序集不在任何引用目录中

c# - 在 Visual Studio 2013 中从 .NET 3.5 迁移到 4.5.1

c# - 如何使用非托管导出 (Robert Giesecke) 将结构数组从 .NET 传递到 Delphi?

c# - 如何在 OpenCvSharp 中获取一条线上的所有点?

c# - 解析 XSLT 中的处理指令

c# - 使用 ComboBox 更改 CollectionViewSource 过滤器