2014-01-09 152 views
0

我試圖在Windows Phone應用程序加載圖像

這裏從嵌入式資源加載圖像是我的項目設置:

模塊1 = Phone應用程序

單詞數= ClassLibrary.dll

Module1調用Module2爲手機應用程序創建所有數據對象。

當Module2創建對象時,我想從資源加載圖像。

資源是「爲Default.png」,並保存在「圖像」目錄(生成操作=嵌入的資源,複製到輸出目錄=一直拷貝)

我使用的代碼產生一個異常

ex = {"The request is not supported. "} 

下面是代碼:

private void LoadImage() 
{ 
    Assembly curAssembly = null; 

    curAssembly = Assembly.GetExecutingAssembly(); 

    string [] names = curAssembly.GetManifestResourceNames(); 

    // names[0] = "Storage.Images.Default.png" 
    // so I know I am using the correct name 

    Stream resStream = curAssembly.GetManifestResourceStream("Storage.Images.Default.png"); 

    BitmapImage bitMapImage = new BitmapImage(); 

    try 
    { 
    bitMapImage.SetSource(resStream); 
    } 
    catch (Exception ex) 
    { 
    Debug.WriteLine(ex.ToString()); 
    } 
} 

你能幫助一個新手了呢? 感謝

回答

0

想象一下出...

我用加載資源錯了名字......

從陣列中使用的名稱返回在此調用

string [] names = curAssembly.GetManifestResourceNames(); 

它完美的作品

0

步驟來獲取工作:

首先Appraoch

  1. 在VS的項目上單擊右鍵,選擇 「添加新項
  2. 從 「常規」標籤選擇「資源文件
  3. 打開resource_file.resx您剛剛添加到您的項目中並選擇「添加現有項目」,然後選擇您的圖像。
  4. 然後然後在您的LoadImage方法,這樣做:

    私人無效的LoadImage()
    {
              Drawing.Bitmap的BitmapImage = resource_file.name_of_your_image;
    }

注意:在這段代碼中,我假定你選擇你的資源文件的名稱是 「resource_file」

第二種方法

System.Resources.ResourceManager rm = new System.Resources.ResourceManager(this.GetType().Assembly.GetName().Name + ".resource_file", this.GetType().Assembly); 
System.Drawing.Bitmap bmp= (System.Drawing.Bitmap)rm.GetObject("Baba"); 

或使用可使用System.Resources.ResourceReader等方法

+0

謝謝你的方法...我會給這些嘗試...但他們不回答我的問題,爲什麼我無法加載資源,即使資源在模塊內,如GetManifestResourceNames所示? – user3174075