2014-02-05 79 views
1

我有一個自託管web api控制檯應用程序。它包括我作爲訪問鏈接的資源圖像:檢索資源名稱是變量的圖像資源

位圖imgBitmap = Properties.Resources.img2

不過,我想訪問它的變量。即資源的名稱將作爲變量提供,其中包含字符串:「img2」

還有其他問題在這個問題上,但他們似乎沒有工作或有足夠的信息讓我弄清楚如何做到這一點。例如:

Retrieve image resource using string variable in foreach loop

於是,我(但我不確定究竟如何指定這個 - 我已經嘗試了幾種變體)...

 ResourceManager rm = new ResourceManager(
      "Properties.Resources", 
      typeof(Properties.Resources).Assembly); 

     var v1 = rm.GetObject("img2"); 

這給了我運行時間錯誤。 「

」找不到適合指定文化或中性文化的資源確保在編譯時「Properties.Resources.resources」已正確嵌入或鏈接到程序集「OwinWebApi2」中,或者所有衛星組件r equired是可加載和完全簽名的。「

我Resources.Designer.cs看起來像這樣...

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.18408 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace Owin_Test1.Properties { 
    using System; 


    /// <summary> 
    /// A strongly-typed resource class, for looking up localized strings, etc. 
    /// </summary> 
    // This class was auto-generated by the StronglyTypedResourceBuilder 
    // class via a tool like ResGen or Visual Studio. 
    // To add or remove a member, edit your .ResX file then rerun ResGen 
    // with the /str option, or rebuild your VS project. 
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 
    internal class Resources { 

     private static global::System.Resources.ResourceManager resourceMan; 

     private static global::System.Globalization.CultureInfo resourceCulture; 

     [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 
     internal Resources() { 
     } 

     /// <summary> 
     /// Returns the cached ResourceManager instance used by this class. 
     /// </summary> 
     [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 
     internal static global::System.Resources.ResourceManager ResourceManager { 
      get { 
       if (object.ReferenceEquals(resourceMan, null)) { 
        global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Owin_Test1.Properties.Resources", typeof(Resources).Assembly); 
        resourceMan = temp; 
       } 
       return resourceMan; 
      } 
     } 

     /// <summary> 
     /// Overrides the current thread's CurrentUICulture property for all 
     /// resource lookups using this strongly typed resource class. 
     /// </summary> 
     [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 
     internal static global::System.Globalization.CultureInfo Culture { 
      get { 
       return resourceCulture; 
      } 
      set { 
       resourceCulture = value; 
      } 
     } 

     /// <summary> 
     /// Looks up a localized resource of type System.Drawing.Bitmap. 
     /// </summary> 
     internal static System.Drawing.Bitmap img2 { 
      get { 
       object obj = ResourceManager.GetObject("img2", resourceCulture); 
       return ((System.Drawing.Bitmap)(obj)); 
      } 
     } 

     /// <summary> 
     /// Looks up a localized string similar to &lt;!DOCTYPE html&gt; 
     /// 
     ///&lt;html lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; 
     ///&lt;head&gt; 
     /// &lt;meta charset=&quot;utf-8&quot; /&gt; 
     /// &lt;title&gt;&lt;/title&gt; 
     ///&lt;/head&gt; 
     ///&lt;body&gt; 
     /// Page1, now in resource editor 
     ///&lt;/body&gt; 
     ///&lt;/html&gt;. 
     /// </summary> 
     internal static string Page1 { 
      get { 
       return ResourceManager.GetString("Page1", resourceCulture); 
      } 
     } 
    } 
} 

回答

1

創建資源管理器時,您需要包含完整的命名空間。而不是

new ResourceManager(
    "Properties.Resources", 
    typeof(Properties.Resources).Assembly); 

應該

new ResourceManager(
    "Owin_Test1.Properties.Resources", 
    typeof(Properties.Resources).Assembly); 

你可以看到,如果你看看Properties.ResourcesResourceManager財產。

+0

非常感謝 - 工作正常 – spiderplant0