KeyedCollection and d:DataContext Design Error

别说谁变了你拦得住时间么 提交于 2019-12-18 17:37:15

问题


See the update below for VS2013.

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.

Parameter name: instantiation

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; }
    }
}

I'm unable to provide design time shape with any class that exposes a KeyedCollection.

Any ideas what is going on here?


Update: As of VS2013 the behavior of the designer in dealing with a KeyedCollection has changed (though still not fully working).

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.

Consider the following example:

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; }
    }
}

回答1:


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

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.

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.




回答2:


If I have'nt misunderstood your question try it like

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication1"
    Width="800" Height="800"
    Title="MainWindow"   
    >
<Window.DataContext>
    **<local:MyClass />**
</Window.DataContext>
<Grid x:Name="LayoutRoot">
</Grid>

I hope this will help.




回答3:


I had to remove the Default Constructor from my DesignInstance-Class



来源:https://stackoverflow.com/questions/15030381/keyedcollection-and-ddatacontext-design-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!