2014-04-04 18 views
0

我有風格的文本像這樣的:如何將樣式文本轉換爲C#對象,例如class/hashTable/collection?

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

我需要編輯在服務器本文(變化的背景圖像或添加顏色屬性...),然後將其保存爲文本。

我認爲最好的辦法是將這些內容轉換爲C#對象,類/ hashTable中/集...

能有人幫我這個問題?

謝謝。

回答

0

使用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類可以重複並沒有唯一性保證。

+0

請添加一些使用示例。你的回答並不直接解決目前OP的問題。 –

+0

嗨raja,你可以請寫一個samll示例代碼如何將樣式文本轉換爲集合/ ...謝謝 –

1

我的建議是儘可能少地保留C#代碼中的樣式信息。最好是在你的CSS文件中定義不同的類,以對應不同的樣式,然後僅處理類名稱服務器端。

+0

感謝您的回答....但它是一個非常複雜的系統,我必須這樣做 –