对象初始化程序中的HtmlGenericControl属性 [英] HtmlGenericControl Attributes in Object Initializer
问题描述
我正在重构一些旧代码,将旧状态图像替换为Font Awesome Icons.
I'm refactoring some of our old code, I'm replacing our old status images as Font Awesome Icons.
我们有一个创建图像并返回图像的方法,用于将控件动态添加到页面.
We have a method that creates an image and returns it, for dynamically adding controls to a page.
旧代码
return new Image {
ImageUrl = SomeConstURL,
ToolTip = tooltip
};
新代码
return new HtmlGenericControl
{
InnerHtml = IconFactory("fa-circle", UnacceptableIconClass, "fa-2x"),
Attributes = { Keys = tooltip}
};
当我使用上面的新代码时,我得到了错误:
When I use the above new code I get the error:
错误638属性或索引器'键'不能分配给它-只读的
Error 638 Property or indexer 'Keys' cannot be assigned to -- it is read only
这是一个直接的错误,它是只读的,我不能这样分配.
It's a straight forward error, it's read only, I can't assign it that way.
我过去曾经这样做过:
someIcon.Attributes["title"] = "My tooltip text";
但是,当我尝试在初始化程序中执行相同的操作时:
However when I try and do the same inside the initializer:
new HtmlGenericControl
Attributes["title"] = "My tooltip text"
}
我得到了错误:
无效的初始值设定项成员delcarator
Invalid initializer member delcarator
我只是不知道如何在初始化程序中执行此操作.
I just have no idea how to do this in the initializer.