wpf 資源使用

2021-10-09 14:05:31 字數 1611 閱讀 7227

string x:key=

"str"

>

沉舟側畔千帆過,並書籤豆腐.

<

/sys:string>

double x:key=

"dbl"

>

3.1415926

<

/sys:double>

<

textblock x:key=

"res1" text=

"海上公升明月"

/>

<

textblock x:key=

"res2" text=

"海上生明月"

/>

<

/resourcedictionary>

<

/window.resources>

資源使用分為靜態方式與動態方式

靜態方式: 在程式載入記憶體時,對資源的一次性使用,之後不再訪問資源

動態方式: 程式在訪問中,仍然會訪問資源。

下面例子演示,分別動靜兩種方式使用資源。當改變資源值時,靜態方式下不會更新,動態方式下會更新。

前台**

<

button margin=

"5,5,5,0" content=""/

>

<

button margin=

"5,5,5,0" content=""/

>

<

button margin=

"5,5,5,0" content=

"update" click=

"button_click"

/>

後台**

private

void

button_click

(object sender,

routedeventargs e)

;this

.resources[

"res2"]=

newtextblock()

;}

如果二進位制資源是string 型別,可以使用應用程式properties命名空間中的resources.resx資源檔案。使用時,

internal改為public,buildaction=embedded resource

訪問資源

例如,把檔案放在專案下 img資料夾中

後台**呼叫方式

///載入二進位制資源 **中呼叫資源

;///**中呼叫資源

uri uri =

newuri

(, urikind.absolute)

;this

.image4.source =

newbitmapimage

(uri)

;

WPF資源字典使用

資源字典出現的初衷就在於可以實現多個專案之間的共享資源,資源字典只是乙個簡單的xaml文件,該文件除了儲存希望使用的資源之外,不做任何其它的事情。1.建立資源字典 resourcedictionary xmlns xmlns x style x key mybtnstyleone targettyp...

WPF 資源字典

使用好處 儲存需要被本地話的內容 錯誤訊息字串等,實現軟編碼 減少重複的 重用樣式,實現多個專案之間的共享資源 修改乙個地方所有引用的地方都會被修改,方便統一風格 使用方法,歸納起來主要有下面幾個步驟 a.建立資源字典檔案,b.資源字典整合 c.使用字典中的資源 說明 在建立資源的時候要確保資源檔案...

WPF筆記1 資源

一 資源的應用場景 style targettype textblock x key titletext setter property background value blue setter property fontsize value 12 style style targettype te...