Windows Phone Build Action的错误

在WP开发当中我们常常会遇到图片无法显示,视频、音乐无法播放,可地址明明是正确的啊,究其原因呢,经过MSDN考证原来是Build Action的设置不当引起的。

在资源文件上点击右键,选择Properties即打开属性窗口,如下图:

Windows Phone Build Action的错误

在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 theirBuild Actionset toContentand notResource.

详细请参考Performance Considerations in Applications for Windows Phone