【发布时间】:2015-05-22 21:35:44
【问题描述】:
我正在尝试在 .NET 4.0 客户端配置文件中构建自定义 Panel 子类。我是这样开始的:
public class MyPanel : Panel
{
public MyPanel()
{
}
}
这可以很好地与 XAML 中的一些子控件集成(local 是 MyPanel 所在的命名空间的前缀):
<local:MyPanel>
<Button/>
<CheckBox/>
</local:MyPanel>
现在,我想向MyPanel 添加一个集合属性。因此,我扩展了这个类:
public class MyPanel : Panel
{
public MyPanel()
{
}
private readonly List<Button> someList = new List<Button>();
public IList<Button> SomeList {
get {
return someList;
}
}
}
到目前为止,一切顺利,上面的 XAML 代码仍然可以编译。
但是,我想在 XAML 中向 SomeList 属性添加一些元素,所以我写:
<local:MyPanel>
<local:MyPanel.SomeList>
<Button/>
</local:MyPanel.SomeList>
<Button/>
<CheckBox/>
</local:MyPanel>
不幸的是,这不再编译,因为编译器输出以下错误:
Bei der Eigenschaft "SomeList" handelt es sich um eine schreibgeschützte IEnumerable-Eigenschaft。 Das bedeutet, dass "IAddChild" von "MyNamespace.MyPanel" implementiert werden muss。 Zeile 9 位置 4. (MC3030)
英文(根据Unlocalize):
MC3030:“SomeList”属性是只读的 IEnumerable 属性,这意味着“MyNamespace.MyPanel”必须实现 IAddChild。
显然,这是指System.Windows.Markup.IAddChild interface。没问题,似乎不太复杂 - 所以,我在MyPanel 中实现了IAddChild(首先以一种没有任何用处的方式开始,但这不重要,因为这些方法不会被执行在应用程序编译之前):
public class MyPanel : Panel, IAddChild
{
public MyPanel()
{
}
private readonly List<Button> someList = new List<Button>();
public IList<Button> SomeList {
get {
return someList;
}
}
public void AddChild(object value)
{
throw new NotImplementedException();
}
public void AddText(string text)
{
throw new NotImplementedException();
}
}
这应该可以,但是...... 不,它没有!我在编译时仍然遇到同样的错误 MC3030。
我完全按照错误信息的指示做了,但是错误并没有消失。我是否遗漏了编译器对我保密的任何其他修改?
documentation on IAddChild 似乎没有提及与这种情况相关的任何内容。此外,谷歌搜索连接到IAddChild 或WPF 的MC3030 只会显示aforementioned Unlocalize entry 作为唯一相关结果。显然,错误 MC3030 是一个非常难以理解的错误,迄今为止很少有开发人员遇到过。
【问题讨论】:
标签: .net wpf xaml .net-4.0 compiler-errors