对内容资源调用的 Application.GetResourceStream 仍返回 null [英] Application.GetResourceStream called on a Content Resource still return null

查看:27
本文介绍了对内容资源调用的 Application.GetResourceStream 仍返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

正在从 DummyMediaLibProvider.cs 执行代码:

公共类DummyMediaLibProvider:IMediaLibProvider{...流资源信息专辑艺术占位符 =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 为内容 Uri 返回 null 的副本因为上一个有扩展名(特别是 .xml)相关的问题.

解决方案

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

>

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

应该是正确的路径.您也可以尝试使用完整的包 URI.(参见 MSDN)

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

Here is the task-related part of the VS2010 project (Windows Phone) structure:

The code is being executed from DummyMediaLibProvider.cs:

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

artwork.placeholder.png Build Action is set to Content.

Still, whenever I run the code, Application.GetResourceStream returns null.

What may be the reason for the resource not being read to memory?

I have attempted to delete obj directory of the project, did Clean and Rebuild, but so far nothing helped.

Update:

If I apply Build Action: Resource to artwork.placeholder.png, I can get the resource stream ok though.

P.S. This is not the duplicate of Application.GetContentStream returns null for content Uri since the last had the extension (particurarly .xml) related problem.

解决方案

The path supplied Application.GetResourceStream isn't relative to the position of the class, but relative to the application package.

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

Would be the correct path. You can also try with a full pack URI. (see MSDN)

And finally, Resource would be the correct Build Action for this.

这篇关于对内容资源调用的 Application.GetResourceStream 仍返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆