Windows Phone Build Action(2)
在WP開發當中我們常常會遇到圖片無法顯示,視頻、音樂無法播放,可地址明明是正確的啊,究其原因呢,經過MSDN考證原來是Build Action的設置不當引起的。
在資源文件上點擊右鍵,選擇Properties即打開屬性窗口,如下圖:

在WP資源文件的Build Action中,我們經常會用到下面幾種情況:
- None:此文件將被忽略不參與編譯和輸出
- Compile:文件將被編譯為Microsoft 中間語言(IL),如文件擴展名為cs的文件
- Content:文件將會打包進xap文件當中,以獨立文件使用
- Page:在WP當中,xaml頁面文件都屬于Page,除App.xaml文件外
- Resource:文件將被編譯到xap文件包的.dll文件內
我們該如何選擇資源文件的Build Action?
在講之前我們不得不說一下關于在Content和Resource情況下訪問資源文件的路徑方式;
- Content:訪問資源文件的路徑方式相對簡潔一些,如"/../.."
- Resource:訪問資源文件的路徑方式繁瑣一些,如"/PhoneApp1;component/Images/back.png",PhoneApp1是項目名稱,component是固定名稱
圖片文件
上一節當中講到過在WP當中,ApplicationBar與SilverLight的層次是完全分開的,那么這就要分兩種情況了:
ApplicationBar的圖標文件應設置為Content方式,SilverLight層次當中應設置為Resource的方式。
多媒體文件
比如音樂,視頻文件,這些資源文件應設置為Content方式。
原因是什么呢?微軟給出的解釋是:
Set Build Action to Content for Media
Media processing on Windows Phone is optimized to use files and network streams, but not in-memory streams. This means that any media files included in the application, such as sound effects, should have their Build Action set to Content and not Resource.
詳細請參考Performance Considerations in Applications for Windows Phone

浙公網安備 33010602011771號