KeyedCollection 和 d:DataContext 设计错误 [英] KeyedCollection and d:DataContext Design Error

查看:19
本文介绍了KeyedCollection 和 d:DataContext 设计错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的 VS2013 更新.

See the update below for VS2013.

将类用作公开 KeyedCollectiond:DesignInstance 时,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.

知道这里发生了什么吗?

Any ideas what is going on here?

更新:从 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.

此外,MSDN 示例d:DesignInstance 用法有时使用没有前缀的类型".在此示例中,如果省略 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屋!

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