我有一个很奇怪的问题。
这是我定义的类:
public class HeaderTagControlsPair
{
public TextBlock HeaderTextBlock = new TextBlock();
public ComboBox TagComboBox = new ComboBox();
public RowDefinition Row = new RowDefinition();
}
现在,我想创建一个此类的对象并对其进行初始化:
HeaderTagControlsPair example = new HeaderTagControlsPair
{
HeaderTextBlock.Text = "test"
};
我做不到。我得到了这三个错误:
Error 1 Cannot initialize type 'CSV_To_Tags_App.HeaderTagControlsPair' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
Error 2 Invalid initializer member declarator
Error 3 The name 'HeaderTextBlock' does not exist in the current context
我不知道为什么会这样,我只是使用简单的对象初始化。我做错了什么?
最佳答案
应该是(C#6):
HeaderTagControlsPair example = new HeaderTagControlsPair
{
HeaderTextBlock = {Text = "test" }
};
关于c# - 初始化我的类的对象时“无法使用集合初始化程序初始化类型”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163123/