Xaml Designer在项目重建后使用旧类型,Item不是Item类型

时间:2018-09-24 13:45:55

标签: c# wpf xaml designer

我在Wpf Xaml ResourceDictionary设计器中遇到了一个讨厌的错误,该错误未在项目中重建类型。也就是说,所有类型都来自先前构建的程序集,因此永远不会在类型比较中匹配。

我有一个项目,该项目的资源字典中包含自定义类型的自定义列表,因此我需要强制转换为这些类型,以使事情正常运行。但是每次我进行更改重建时,设计人员都会保留旧的类型,并使设计时视图无用。

我发现修改列表可以解决该异常,但是在每次构建后都必须对每个自定义类型的列表执行此操作很麻烦,是否有解决此问题的方法?

再现错误的示例代码:

  1. 创建新的Wpf项目。

  2. 添加以下文件...

ItemCollection.cs

using System.Collections.ObjectModel;

namespace XamlTest {
    public class ItemCollection : ObservableCollection<Item> {
    }
    public class Item {
    }
}

ItemsList.xaml (ResourceDictionary)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:XamlTest">
  <local:ItemCollection x:Key="ItemsList">
    <local:Item />
    <local:Item />
    <local:Item />
  </local:ItemCollection>
</ResourceDictionary>
  1. 构建一次。

  2. 通过复制和粘贴并构建来更改列表中Item的数量。

  3. 重复步骤4,直到发生异常。

例外: The value "XamlTest.Item" is not of type "XamlTest.Item" and cannot be used in this generic collection. Parameter name: value

0 个答案:

没有答案