我有這個功能,它工作正常,從這個特定的資源文件中獲得一個名爲OkayMessages的翻譯值。動態初始化ResourceManager
public static string GetResourceString(string resourceKey){
ResourceManager resourceManager = Resources.OkayMessages.ResourceManager;
return resourceManager.GetString(resourceKey);
}
但我有1個多資源文件,我想這個功能,從這些文件中獲取值以及..只是,我在使用動態/程序選擇正確的資源(經理)的麻煩。 我試圖使用下面的代碼,以及一些變種,但我總是得到一個錯誤。
public static string GetResourceString(string resourceFile, string resourceKey){
ResourceManager resourceManager = new System.Resources.ResourceManager("Resources." + resourceFile, Assembly.GetExecutingAssembly());
return resourceManager.GetString(resourceKey);
}
我得到的大部分時間的錯誤是:找不到適合指定文化或中性文化的任何資源。確保「Resources.OkayMessages.resources」正確嵌入或鏈接到程序集..
更新:我使用\ App_GlobalResources文件\文件夾我的資源文件,似乎這就是問題所在。當我在我的項目的根目錄中放置一個資源文件時,我可以初始化一個ResourceManager而沒有任何問題。
所以這裏的根本問題是,在App_GlobalResources文件夾資源編譯到自己的組件(App_GlobalResources文件)。 GetGlobalResourceObject方法知道使用正確的程序集。 –