繁体   English   中英

如何将样式文本转换为C#对象,例如class / hashTable / collection?

[英]How to convert style text to C# object such as class/hashTable/collection?

我有这样的样式文本:

".abc {border: 1px solid blue;color:black;...} 
.abc{background-image:url('http://example.com/images/a.png')...}
#abcd {color: blue}..."

我需要在服务器中编辑此文本(更改背景图像或添加color属性...),然后将其另存为文本。

我认为最好的方法是将文本转换为ac#对象,例如class / hashTable / collection ...

有人可以帮我解决这个问题吗?

谢谢。

我的建议是在C#代码中保留尽可能少的样式信息。 最好在CSS文件中定义与不同样式相对应的不同类,然后仅处理服务器端的类名。

使用CssStyleCollection内置类。

System.Web.UI.WebControls.Style style = new System.Web.UI.WebControls.Style(); 
// you can set various properties on style object.

CssStyleCollection cssStyleCollection = style.GetStyleAttributes(SOME_USER_CONTROL OR YOUR PAGE);
cssStyleCollection.Add("border", "1px solid blue"); // etc

另一种选择是使用以下结构:

List<KeyValuePair<string, List<KeyValuePair<string, string>>> cssValues = new List<KeyValuePair<string, List<KeyValuePair<string, string>>>();

cssValues.Add(new KeyValuePair<string, List<KeyValuePair<string, string>>("abc", new List<KeyValuePair<string, string>>
{
 new KeyValuePair<string, string>("border", "1px solid blue"),
 new KeyValuePair<string, string>("color", "black"),
 // so on
}));

我们使用KeyValuePairs列表而不是字典,因为CSS类可以重复并且不能保证唯一性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM