在内容资源上调用的Application.GetResourceStream仍然返回null

Max*_*lov 10 .net c# embedded-resource windows-phone-7

以下是VS2010项目(Windows Phone)结构中与任务相关的部分:

在此输入图像描述

代码正在从DummyMediaLibProvider.cs执行:

public class DummyMediaLibProvider: IMediaLibProvider
{
    ...
    StreamResourceInfo albumArtPlaceholder = 
        Application.GetResourceStream(
            new Uri("../Images/artwork.placeholder.png", UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

artwork.placeholder.png Build Action设置为Content.

但是,每当我运行代码时,Application.GetResourceStream返回null.

资源无法读取到内存的原因是什么?

我试图删除项目的obj目录,做了清理和重建,但到目前为止没有任何帮助.

更新:

如果我将Build Action:Resource应用于artwork.placeholder.png,我可以获得资源流.

PS这不是Application.GetContentStream的副本,因为上一次有扩展(particurarly .xml)相关问题,因此内容Uri返回null.

Cla*_*sen 21

提供的路径Application.GetResourceStream不是相对于类的位置,而是相对于应用程序包.

StreamResourceInfo albumArtPlaceholder = 
    Application.GetResourceStream(
        new Uri("Images/artwork.placeholder.png", UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

将是正确的道路.您还可以尝试使用完整包URI.(见MSDN)

最后,Resource这将是正确Build Action的.

  • 删除最初的`/`对我来说是什么.我还将我的内容设置为`Content`而不是`Resources`(在构建之后用7-Zip检查xap以验证文件是否包含在内). (6认同)