如何將圖像存儲在類庫中並從任何類訪問它 (How can i store an image in a class library and access it from any class)


問題描述

如何將圖像存儲在類庫中並從任何類訪問它 (How can i store an image in a class library and access it from any class)

我的解決方案中的“圖像”文件夾中有一些圖像。在我的庫中的一個類中,我想決定在運行時應該使用哪個圖像。我會從後面的用戶控件代碼中執行此操作的舊方法是在類的構造函數中加載所有圖像,如下所示:

myImage = FindResource("MyImageResource") as BitmapImage;

然後在運行時確定要使用的圖像。

但這似乎不適用於我的類庫中的任何類,更不用說它似乎違背了我所讀到的關於 MVVM 的所有內容。

關於我正在嘗試做什麼的更多信息基本上我有一個模型類,它存儲了已插入驅動器上的所有信息,例如驅動器類型及其姓名。


參考解法

方法 1:

What i have decided to do thus far is to give the model class a BitmapImage property which i set in it's constructor to the Image that represents its drive type using this line:

DiskImage  = new BitmapImage(new Uri("/MyApp;component/Images/DiskDrive.png", UriKind.Relative));

(by TimmothTimmoth)

參考文件

  1. How can i store an image in a class library and access it from any class (CC BY‑SA 2.5/3.0/4.0)

#visual-studio #mvvm #wpf #C#






相關問題

如何在 C# 中使用帶有動態參數的 INSERT INTO SQL 查詢? (How to use a INSERT INTO SQL Query with Dynamic parameters in C#?)

擴展 SSRS 中的圖表功能 (Extending chart functionality in SSRS)

如何將 MVC 5 項目模板添加到 VS 2012? (How can I add the MVC 5 project template to VS 2012?)

Visual Studio - 將按鈕/複選框添加到工具欄以切換只讀文件屬性, (Visual Studio - Add a button/checkbox to a toolbar to switch the readonly file property,)

在 Javascript/Jquery Ajax 調用中使用 WCF 服務 (Consuming WCF service in Javascript/Jquery Ajax call)

Visual Basic 2013 - 控制台輸入令牌? (Visual Basic 2013 - Console Input Tokens?)

您是否知道用於編輯/翻譯資源(.rc)文件的好的程序? (Do you know of a good program for editing/translating resource (.rc) files?)

ADODB.Recordset 數據無法綁定到 datagridview (ADODB.Recordset data cannot bind into datagridview)

Reporting Services - 對數據源視圖 (DSV) 的報表模型 (SDML) 引用 (Reporting Services - Report Model (SDML) reference to Data Source View (DSV))

從 Visual Studio 2005 遷移到 2008 時要注意什麼? (What to look out for when moving from Visual Studio 2005 to 2008?)

動態改變另一個類的標籤值 (Dynamically changing the value of a label from another class)

在同一文件夾中為 .NET 5 和 .NET 6 Preview 3 構建 WebAssembly 項目 (Building WebAssembly project for both .NET 5 and .NET 6 Preview 3 in same folder)







留言討論