安裝String Resource Generator 1[1].2.5,運行SRGenerator.msi。然後給自己的工程中添加SR.strings檔案,通過VS.NET在現有的.RESX或SR.strings檔案設定Custom tool屬性為:StringResourceTool或SRCodeGen。 使用呢,比如:我們在SR.strings檔案中定義了一行:
ItemNotFoundMessage(key) = Item not found in cache"r"n"tKey: {0}那麼我們在應用程式中就可以這樣:this.primitivesResultsTextBox.Text += SR.ItemNotFoundMessage(selectItemForm.ItemKey) + ""r"n"; 其實,這個SR.ItemNotFoundMessage的定義在自動產生的SR.CS檔案中: public static string ItemNotFoundMessage (string key)
{
return Keys.GetString( Keys.ItemNotFoundMessage, key );
} 這種傳入方式很利於我們統一應用中的錯誤描述的產生方式。比如我們可以定義錯誤描述在SR.strings檔案中:LBS_Request_CannotConnectServer = 無法串連到遠端LBS閘道伺服器:{0}那麼在實際調用時就可以這麼:catch(WebException ex)
{... sb.Append("<result resid=""501"">"+ SR.LBS_Request_CannotConnectServer(_Url) +"</result>"n");...} 其他的SR.Strings定義方式還有:
Name |
Value |
Comments |
Member on SR class |
NotAuthorized |
You are not authorized, go away |
|
String NotAuthorized { get; } |
ArgumentInvalid |
Argument {0} is not valid |
argumentName |
String ArgumentInvalid(object argumentName); |
ConnectionBad |
Connection server: {0}, database {1} invalid |
server,db |
String ConnectionBad(object server, object db); |
OtherStuff |
This {0}, is undefined: {1} |
|
String OtherStuff(object arg1, object arg2); |