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

标签 c# .net windows-phone-7 embedded-resource

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

enter image description here

正在从 DummyMediaLibProvider.cs 执行代码:

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

artwork.placeholder.png Build Action 设置为 Content

仍然,每当我运行代码时,Application.GetResourceStream 都会返回 null。

资源没有读入内存可能是什么原因?

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

更新:

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

附言这不是 Application.GetContentStream returns null for content Uri 的副本因为上次有与扩展(特别是 .xml)相关的问题。

最佳答案

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

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

将是正确的路径。您也可以尝试使用完整包 URI。 ( see MSDN )

最后,Resource 将是正确的 Build Action

关于c# - 在内容资源上调用的 Application.GetResourceStream 仍然返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7394282/

相关文章:

c# - 使用 BYTE* 输出导入 DLL 时出现问题

c# - 如何检查 ViewBag 属性是否为空或不存在

c# - 如何批量更新 Entity Framework 中的记录?

c# - 错误 : NU1100: Unable to resolve 'MicrosoftOfficeCore (>= 15.0.0)' for 'net5.0'

c# - 如何将 PCL 转换为基于 .net 标准的库

c# - Entity Framework |错误 : InvalidCastException: The field of type System. Int32 必须是字符串、数组或 ICollection 类型

windows-phone-7 - Windows Phone 7背景主题设置-应用开发

c# - 窗口电话7错误

c# - 如何在Windows Phone中使用只读数据库?

c# - ShouldSerialize*() 与 *指定的条件序列化模式